Cake PHP Checkboxes

强颜欢笑 提交于 2020-01-14 04:35:08

问题


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:

  1. Field Type = Tinyint
  2. Length/Values = 1
  3. 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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!