Symfony2 - How to set, and get, options when using a Form Class?

后端 未结 6 1538
暖寄归人
暖寄归人 2021-02-06 08:29

I am using Form Classes to build the various form in my project.

In the Entity Type file, for the buildForm function, there is a secondary parameter of \"array $options\

6条回答
  •  温柔的废话
    2021-02-06 09:00

    Apparently it's not with getDefaultOptions() anymore, but with setDefaultOptions().

    Otherwise it says

    The option "my_custom_option" does not exist. Known options are: "action", "attr", "auto_initialize", ...

    So, for me I had to update setDefaultOptions like this :

    public function setDefaultOptions(OptionsResolverInterface $resolver)
    {
        $resolver->setDefaults(array('my_custom_option' => false));
        // Others if needed, like in the documentation for : 'data_class' => 'VENDOR\Bundle\Entity\MyEntity', 'csrf_protection' => true
    }
    

    And then you can retrieve it in the buildForm method

    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $myCustomOption = $options['my_custom_option'];
    }
    

提交回复
热议问题