How to render a checkbox that is checked by default with the symfony2 Form Builder?

后端 未结 13 2150
鱼传尺愫
鱼传尺愫 2020-12-17 09:30

I have not found any easy way to accomplish to simply check a Checkbox by default. That can not be that hard, so what am i missing?

相关标签:
13条回答
  • 2020-12-17 10:31

    Use the FormBuilder::setData() method :

    $builder->add('fieldName', 'checkbox', array('property_path' => false));
    $builder->get('fieldName')->setData( true );
    

    "property_path" to false cause this is a non-entity field (Otherwise you should set the default value to true using your entity setter).

    Checkbox will be checked by default.

    0 讨论(0)
提交回复
热议问题