CakePHP form helper - HABTM multiple checkbox styling - cakephp

I have two tables: "restaurants" and "cuisines" which are related to each other by a HABTM table
The table cuisines has certain fixed entries - 54 number
A restaurant can have any number of cuisines. On baking the application this came with a multiple select. Since i wanted check boxes i used array( 'type' => 'select', 'multiple' => 'checkbox') to convert it into checkboxes.
Now i want to style the way this checkboxes are displayed into columns of 4 as seen on the screenshot below.
img2.pict. com/82/bc/a4/1453459/0/200908111511.png
echo $form->input('Cuisine', array('type' => 'select', 'multiple' => 'checkbox'));
The above code produces many div's around each element as follows
http://img2.pict.com/1a/a3/0a/1453457/0/200908121509.png
I have tried the following:
echo $form->input('Cuisine', array( 'type' => 'select', 'multiple' => 'checkbox', 'div' => false, 'label' => false));
but this code only removes the outside divs and label. I am not able to control the internal
<div class="checkbox">
<label for="CuisineCuisine2">Andhra</label>
that appear around the single checkboxes.
How can I use the FormHelper to remove or give classes to the internal divs, so I can do some custom styling?
Or is there any other way to populate this HABTM table to get the effect i want?

You could get around this by doing $form->select() instead, and apply a style or class attribute to get it to look how you want.
It seems to make sense to not use the $form->input() function if you are going to remove the div and label anyway.

You can stylize the DIV elements with CSS.
<style>
div.input div.checkbox {
float: left;
width: 50%;
}
</style>

You can remove or give classes to the internal divs like this
$this->Form->input("hello_test",array('type'=>'checkbox','div'=>'class_name'));
By default cake uses : type class e.g - type is checkbox then class="checkbox"

Related

Label for Select Form Helper

I"m having trouble with CakePHP3's Form Helper class.
When I create a text input field like this:
echo $this->Form->input('fieldname');
When the input field is "required", a label appears in bold. This is fine.
However, when I create a select field like this:
echo $this->Form->select('fieldname', [1,2,3,4,5]);
The select field is created however there is no label at all. I can add it manually using:
echo $this->Form->label('fieldname');
However in cases where the select is a required field, the font is not correct (required labels show up bold with a red * following them). I've narrowed this down to the CSS only applying to nested label tags (so when I create a label tag, it's not inside a div tag like the labels for the text inputs are).
I'd like to avoid manually inserting HTML code to achieve my desired result, any help would be appreciated!
<?= $this->Form->input('fieldName', ['type' => 'select', 'options' => ['0' => 'Option1' , '1' => 'Option2'], 'empty' => __('(choose one)'), 'label' => __('LabelName')]);?>
or
$sizes = ['s' => 'Small', 'm' => 'Medium', 'l' => 'Large'];
<?= $this->Form->input('fieldName', ['type' => 'select', 'options' => $sizes, 'default' => 'm', 'label' => __('LabelName')]);?>

Different Title for Different Radio buttons in Cakephp form helper

How can i give different title for different radio button using CakePHP Form Helper
$radio_options = array('unknown'=>'Unknown','negative'=>'Negative','positive'=>'Positive');
$titles = array('0'=>'Unknown','1'=>'Negative','2'=>'Positive');
I am trying to create radio buttons like this
echo $this->Form->input('radio_buttons', array(
'options' => $radio_options,
'legend' =>false,
'label' => true,
'div'=>false,
'class'=>'radio inline',
'type' => 'radio',
'separator'=>'<br>',
'title'=>$titles,));
But its not working..Form Helper creating same title for all of the radio buttons.
Look at what the FormHelper generates, then generate it manually or with a php foreach loop or something.
There are cases like this, when it's just easier (or the only way) to not use a helper.

Is it possible to create a checkbox with images along with the text in the labels in cakephp?

Somebody please give me ideas on this!I would like to generate multiple checkboxes with an image along with the text in the label. I have created a field called solutioncheckbox in the Contact form for Checkbox. My code is as below for creating multiple Checkboxes
<?php echo $this->Form>input('solutioncheckbox',array('label'=>false,'type'=>'select','class'=>'solution','multiple'=>'checkbox','options'=>array(1=>'WEB DESIGN',2=>'WEB DEVELOPMENT',3=>'GRAPHICS DESIGN'))) ;?>
Can I create an image along with the text in each of the labels like WEB DESIGN,WEB DEVELOPMENT etc in my form
This will get the job done for you ;)
echo $this->Form->input
(
'Model.field',
array
(
'multiple' => 'checkbox',
'options' => array('1' => $this->Html->image('cake.icon.png') . 'Text beside the image', '2' => $this->Html->image('test-error-icon.png') . 'Other text beside the image'),
'escape' => FALSE
)
);

How to change the separators for Cakephp Form Helper for multiple checkboxes

I'm generating a bunch of checkboxes through the Form Helper.
Essenciatially I have an array with like $tests = array
$tests = array(1 => 'test', 15=>'test2');
Then I can use it like this
echo $this->Form->input('test_id', array(
'type' => 'select',
'multiple' => 'checkbox',
'div' => false,
'before' => '<li>',
'after' => '</li>',
'separator' => '</li> <li>'));
I expected it would use the div => false to take off the div of every checkbox but it only applies the options to the exterior block. Is there anyway to change all the blocks from <div class=>'checkbox'> to <li class='anything else'>
Just look at the fields it produces, then write your own simple foreach() loop and write them yourself in whatever wrapping element(s) you want.
I forget whether it's possible with Cake or not, but don't think it is. The above is what we've done before - because it's simple to write, it took us less time to write than it did to look further into it :)

Cake PHP 2 custom Form->Label

I'm using the Form helper to generate a label:
$this->Form->label('Contact.name', 'Name');
Which generates the following:
<label for="ContactName">Name</label>
Is it possible to generate the following using the helper:
<label for="ContactName"><span class="mandatory">*</span> Name</label>
Whilst I can manually write the html for the above it becomes a little more difficult when I am using the input method where a label is automatically generated.
For example:
$this->Form->input('Contact.forename',array('div' =>false,
'label' => array(
text'=> 'First Name',class =>'myclass'),
'class' => 'input','size' => '25' ,'tabindex' => '1'));
Is this possible in cake or do I have to manually inject the html using javascript when the page loads? Which I would think is rather ugly.
If you are using model validation for the mandatory fields then cakephp automatically applies '*' on the Label else you can use the helper as follows-
echo $this->Form->label('name', '<span class="mandatory">*</span> Name');
If you don't want the labels to generate automatically you can use "label => false" while using the helper.
echo $this->Form->input('Contact.forename',array('label' =>false));
Not sure CakePHP supports that (and it would get a bit messy anyway). The simplest solution I can think of is to assign a "mandatory" class to the label via the form helper:
$this->Form->label('User.name', 'Your username', array('class'=>'mandatory'));
Which produces something like:
<label class="mandatory" for="ContactName">Name</label>
Then the rest is done purely in CSS:
label.mandatory:after {
content: ' *';
color: red;
display: inline;
}
Avoids having any additional HTML.
I know this is old, but maybe someone with Cakephp 3 is having the same problem. This is what fixed it for me, without any inlinecode.
<?php
echo $this->Form->input(
'renovate_old',
[
'type' => 'checkbox',
'label' => ['text' => __('Alte Wohnung'), 'class' => 'moCheckLabel']
]); ?>
So you can name your label and use the Databasefield to write.
you can do it simple by
echo $this->Form->input('whatever', array('between'=>'<label for="ContactName"><span class="mandatory">*</span> Name</label>','label'=>false));

Resources