php 选择字段默认值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/35771945/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 00:34:26  来源:igfitidea点击:

Choice field default value

phpsymfonysymfony3.x

提问by Alexander Lomia

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 ?

如何在呈现表单时默认选中“朋友”复选框?

回答by BENARD Patrick

I think you should try with dataoption, 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.

我认为您应该尝试使用data选项,但只是在您甚至没有在对象中保存数据的情况下,因为它会覆盖它。

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 datawith empty_dataif you need in any case to update default value

作为下面的答案,如果您需要在任何情况下更新默认值,请替换dataempty_data

回答by Romain Bruckert

Use the empty_dataform field option. (not databecause it will override any posted data unless you set it dynamically).

使用empty_data表单域选项。(不是data因为它会覆盖任何发布的数据,除非您动态设置它)。

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.

复杂情况的另一个选择是使用Sf 动态表单事件

回答by Fabien Salles

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'
            ]);
    });
}

回答by Shadi Akil

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());
}