Cake PHP Checkboxes - cakephp

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]);

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')]);?>

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

display default time in cakephp ctp

i wish to display default time as 9:00 am in ctp time dropdown. Following is my ctp code:
<?php
echo $this->Form->input('Rideoffer.DepartureTime', array(
'type' => 'time',
'interval' => 5
));
?>
how do i do that?
Use the 'selected' option
<?php
echo $this->Form->input('Rideoffer.DepartureTime', array(
'type' => 'time',
'interval' => 5,
'selected' => '09:00:00',
));
?>
The best way to set default data is using the controller (and only if not posted):
if (!$this->request->is('post')) {
$this->request->data['Rideoffer']['DepartureTime'] = '09:00:00';
}
see http://book.cakephp.org/2.0/en/core-libraries/helpers/form.html#common-options
This works in all form elements.
"selected", "value", "checked" and other hardcoded attributes for the form directly usually break the form after an unsuccessful post (if the form contains validation errors): it loses all the entered data and reset them to the value it was before which is usually quite annoying for the frontend user.
see http://www.dereuromark.de/2010/06/23/working-with-forms/

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

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.

Can't avoid field label to be echoed

I've been creating an app preparing it already to be internationalized. But when I try to do this:
echo $this->Form->input('end_date', array('label' => __('End Date'), 'dateFormat' => 'DMY', 'minYear' => date('Y'), 'type' => 'text'));\
It echoes two "End Date". I tried disabling label by setting it to null, but didn't work either. :(
How can I avoid this to happen?
Thanks
Instead of NULL, try setting it to false:
'label' => false
You can also set ALL inputs of a form to default to 'label'=>false by using:
'inputDefaults'=> array('label'=>false)
as an option of your form
For what it's worth, the __() function echoes its value by default instead of returning it. That's why you were seeing the label displayed twice. It was being displayed once because that's the value that the field name resolves to automagically. It was being displayed the second time by the __() method. In other words, your label option wasn't really overriding the automatic label.
echo $this->Form->input(
'end_date',
array(
'label' => __('Modified End Date Label', true), # note the "true" argument
'dateFormat' => 'DMY',
'minYear' => date('Y'),
'type' => 'text'
)
);
For more, see the __ documentation.

Resources