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

后端 未结 13 2252
鱼传尺愫
鱼传尺愫 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:23

    You should make changes to temporary object where entity is stored before displaying it on form. Something like next:

    getDisplayed() === null) {
                $options['data']->setDisplayed(true);
            }
    
            // ...
    
            $builder
                ->add('displayed', 'checkbox', array(
                    'required' => false
                ));
        }
    }
    

提交回复
热议问题