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

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

    UserBundle\Entity\User
    

    let's assume that you have an entity called ( User ) and it has a member named isActive, You can set the checkbox to be checked by default by setting up isActive to true:

    $user = new User();
    
    // This will set the checkbox to be checked by default
    $user->setIsActive(true);
    
    // Create the user data entry form
    $form = $this->createForm(new UserType(), $user);
    

提交回复
热议问题