问题
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));?>
回答1:
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'
)
));
回答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!
回答3:
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'));
回答4:
CakePHP 3.0
$this->Form->input('id', ['type'=>'select', 'multiple' => 'checkbox', 'options'=>$array]);
来源:https://stackoverflow.com/questions/13947986/cake-php-checkboxes