Choice field default value

让人想犯罪 __ 提交于 2020-01-20 04:29:04

问题


I have the following form:

public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder
    ->add('type', ChoiceType::class, array(
        'expanded' => true,
        'multiple' => false,

        'choices' => array(
            'Friend' => 'friend',
            'Guide' => 'guide'
        )
    ));
}

How can I make 'Friend' checkbox to be checked by default when the form is rendered ?


回答1:


I think you should try with data option, but it's just in the case where you don't even have a data saved inside your object, because it will override it else.

Important : It's good for create action, but not for edit action.

 public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
        ->add('type', ChoiceType::class, array(
            'expanded' => true,
            'multiple' => false,

            'choices' => array(
                'Friend' => 'friend',
                'Guide' => 'guide'
            ),
            'data' => 'friend'
        ));
    }

Official link

Extract :

When you create a form, each field initially displays the value of the corresponding property of the form's domain object (if an object is bound to the form). If you want to override the initial value for the form or just an individual field, you can set it in the data option

UPDATE If YOU NEED EMPTY VALUE:

As the answer below, replace data with empty_data if you need in any case to update default value




回答2:


Use the empty_data form field option. (not data because it will override any posted data unless you set it dynamically).

public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder
    ->add('type', ChoiceType::class, array(
        'expanded' => true,
        'multiple' => false,

        'choices' => array(
            'Friend' => 'friend',
            'Guide' => 'guide'
        ),
        'empty_data' => 'friend'
    ));
}

Another option for complex cases is to use Sf Dynamic Form Events.




回答3:


If you don't want to override value for an edition you can do this :

public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder->addEventListener(FormEvents::PRE_SET_DATA, function (FormEvent $event) {
        $form = $event->getForm();

        $form->add(
            'type', 
            ChoiceType::class,
            [
                'expanded' => true,
                'multiple' => false,
                'choices' => [
                    'Friend' => 'friend',
                    'Guide' => 'guide'
                ],
                'data' => $event->getData() ?: 'friend'
            ]);
    });
}



回答4:


I think it would be better to set initial values in the Entity constructor:

public function __construct()
{
    $this->exercises = new ArrayCollection();

    $this->setTitle("WOCHE#") ;

    $this->setYear(date('Y'));

    $this->setWeekInCalendar(Week::getCurrentWeekInCalendar());
}


来源:https://stackoverflow.com/questions/35771945/choice-field-default-value

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