Symfony 2表单的默认选项被覆盖而不合并 [英] Default Options for symfony 2 forms are being overridden not merged

查看:96
本文介绍了Symfony 2表单的默认选项被覆盖而不合并的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个自定义表单类型,该类型定义了一些默认的attr选项:

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

但是,当我使用此自定义表单类型时,整个attr数组都将替换为定义的内容.

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'
     ),
 ));

呈现表单时,它只有<input data-label="Logo" ...>

如何获取这些选项,以使这些选项不会完全被覆盖?

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

推荐答案

您可以在作为自定义类型buildForm方法的第二个参数传递的options数组中找到它们.您可能想要执行以下操作:

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';
    ...

这篇关于Symfony 2表单的默认选项被覆盖而不合并的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆