Multiple checkboxes in CakePHP - how to set which are checked? - cakephp

I have multiple checkboxes in CakePHP's Add/Edit view, created with:
echo $this->Form->input('email_warning_chb', array('type'=>'select', 'multiple'=>'checkbox', 'label'=> __('Email notice'), 'class'=>'multiple-chb', 'options'=> array('title...'=>array( '5'=>'5 days', '15'=>'15 days', '30'=>'30 days', '60'=>'60 days');
My question is how to set which one are checked by default (ie. in thi example, 5, 15 and 60)?
Thank you in advance!

As said in other answers, you should set the 'selected' option.
What some people don't mention is that your selected array should only contain the id in each element.
Example:
$selectedWarnings = $this->Warning->find('list', array(
'fields' => array('id')
));
echo $this->Form->input('email_warning_chb', array(
'label' => 'Email Notice',
'type' => 'select',
'multiple' => 'checkbox',
'options' => $warnings,
'selected' => $selectedWarnings
));

this looks like this one
cakephp: How to set checkbox to checked?
where $selected contains the selected values

in your controller you have to put the value like this:
$this->request->data['Model']['email_warning_chb'] = array(5,15,60);
and it will automatically display checkbox as selected.
Please ask if not work for you.

Related

Expression Engine 3: Fieldtype settings - checkboxes

I'm working in Expression Engine 3 and I'm working on a custom field type, specifically the options for said field type. I've been following the docs along with how to build out the form for the settings, and everything is rendering correctly.
My question is with regard to checkboxes (and radio buttons), how do I determine which boxes should be pre-selected based on the currently saved settings?
The code I have for outputting the checkboxes is as follows:
$settings = array(
array(
'title' => 'Select your option(s):',
'fields' => array(
'multi' => array(
'type' => 'checkbox',
'choices' => array(
'1' => 'Option 1',
'2' => 'Option 2'
)
)
)
),
Now, in the display_settings($data) function, if I do a var_dump($data) I can see the values being correctly pulled in. But the checkboxes will not automatically set themselves to selected if they should be.
Any advice would be lovely, thank you! Also, I'd be happy to provide more specific details if needed.

How to use empty and selected in same dropdown box in cake php

I'm editing a dropdown box. i need to use empty and selected in same dropdown.
here is my code
$this->Form->input('per', array('id'=>'per','class'=>'inputs con_field','label'=>'per :', 'type'=>'select','options'=>$per_values,'selected'=>$labrcfps['Labourcfps']['per'],'empty'=>'- - Select --'));
If i use empty it automatically takes the empty as selected value.
I'm new in cakephp but i do this with array and parameter defaults :)
like that:
$options = array(
'x' => 'Select',
1 => 'Something',
2 => 'Car',
3 => 'Train'
);
echo $this->Form->input('name', array(
'options' => $options,
'default' => 'x'
));
Maybe that will be working fine :)
Variable $options is just your options after that you can validate if name of that field has value integer or 'x'. If 'x' then deny rest of logical code

Form element array cakephp

I am in need of all the arrays and subarrays for a form element. When referred cookbook,
I found only the subarrays for limited elements.
For example,
<?php
echo $this->Form->input('name', array(
'div' => array(
'id' => 'mainDiv',
'title' => 'Div Title',
'style' => 'display:block'
)
));
?>
Here in this form helper, we can get only arrays like id, title, style.But I am in need of all the possible array keys for form elements. How can we get this?
You can set common properties of any form while creating it like below which will be applicable for all form elements
echo $this->Form->create('my_form', , array(
'inputDefaults' => array(
'label' => false,
'div' => false
));
Now the label and div will not appear to any form element you gonna use, This is how you can create common set of array for all form element under it..
Hope this will help u

Cake PHP Checkboxes

I am new to CakePHP now I'm working on checkbox I used the following statement but it
gives check box after the label and it prints the field also.My requirement is it does not
print the field name and label should be displayed after the check box.
please help me ,
Thanks in advance
<?php echo $form->input('Model.name', array('multiple' => 'checkbox', 'options' =>
$options, 'selected' => $selected));?>
First, make sure your value is a boolean or tinyint. Otherwise, you will never get a checkbox.
Then, just build like this :
echo $this->Form->input('Model.field', array(
'type' => 'select',
'multiple' => 'checkbox',
'options' => array(
'Value 1' => 'Label 1',
'Value 2' => 'Label 2'
)
));
My solution is according to v.2.0
<?php
echo $this->Form->input('field_name', array(
'label' => 'Some label',
'selected' => $selected
/*maybe some other options*/
));
?>
if you've specified model name above, while creating the form, you dont need to use name of model . If field is boolean, you'd get the control as checkbox automatically. Alsom you can specify it in options array like
'type'=>'checkbox'
good luck!
To draw a check box you have to first configure your table in DB properly. Set these options on your field in DB:
Field Type = Tinyint
Length/Values = 1
Set Defualt = 0
and finally your view:
echo $this->Form->input('checkbox_field');
100% will work if not then set default value for your field in view:
echo $this->Form->input('checkbox_field', array('type'=>'checkbox'));
CakePHP 3.0
$this->Form->input('id', ['type'=>'select', 'multiple' => 'checkbox', 'options'=>$array]);

Give ID to submit button

I have a form and i have ended it using,
echo $this->Form->end('Login');
However, i can't seem to assign it an id or class to style it and be able to reference it with jquery. Also,I want to style it with a picture and the picture has the text already in so i need the buttons value to be empty but i can't manage that either...
Any Help?
Many Thanks, Chris
***Note - this is the html it generates;
<div class="submit"><input type="submit" value="Login"/></div>
Ideally i would like to remove the div aswell but its not a necessity.
here's the solution:
$options = array
(
'label' => 'Update',
'value' => 'Update!',
'id' => 'blabla',
'div' => array(
'class' => 'glass-pill',
)
);
echo $this->Form->end($options);
Cheers! =)

Resources