-->

Default Options for symfony 2 forms are being over

2019-06-04 07:17发布

问题:

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