Symfony中复选框的默认值

时间:2017-10-31 18:40:02

标签: symfony

如果选中该复选框,变量是否取值1,如果未选择值0?

$builder
    ->add('happy', 'checkbox', array(
        'label' => 'Are you happy?',
        'data' => false,
    ));

在这个链接中,我不清楚:

  

实际用作复选框或单选按钮值的值。这不会影响您在对象上设置的值。

     

https://symfony.com/doc/current/reference/forms/types/checkbox.html

感谢您的回答!

2 个答案:

答案 0 :(得分:2)

试试这个,

$builder
      ->add('happy', 'checkbox', array(
      'label'=>'Are you happy?',
      'data'=>false,
      'attr' => array('checked' => 'checked', 'value' => '1')
      ));

答案 1 :(得分:1)

您的意思是表单提交的返回值?通过链接页面,它看起来应该返回一个布尔值:

if the box is checked, the field will be set to true, if the box is unchecked, the value will be set to false.

可以肯定的是,您总是可以尝试转出$form->getData()

的结果
相关问题