Default Options for symfony 2 forms are being overridden not merged

别说谁变了你拦得住时间么 提交于 2019-12-08 04:55:03

问题


I have a custom form type that defines some default attr options:

public function setDefaultOptions(OptionsResolverInterface $resolver)
{
    $resolver->setDefaults(array(
        'invalid_message' => 'The selected image does not exist',
        'attr'=>array(
            'data-image-picker'=>'true',
            'data-label'=>'Pick Image'
        ),
    ));
}

However when i use this custom form type the entire attr array is replaced with what is defined.

$builder->add('logo','image_picker',array(
    'attr'=>array(
        'data-label'=>'Logo'
     ),
 ));

When the form is rendered it only has <input data-label="Logo" ...>

How do i get it so that those options will be merged not completly overridden?


回答1:


You can find these in the options array passed as the second argument to the buildForm method of your custom type. You would want to do something like this:

public function buildForm(FormBuilderInterface $builder, array $options)
{
    $options['attr']['data-label'] = 'Logo';
    ...


来源:https://stackoverflow.com/questions/27431524/default-options-for-symfony-2-forms-are-being-overridden-not-merged

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