24
4

3 に答える 3

67

In your form add a text field with a false property_path:

$builder->add('extra', 'text', array('property_path' => false));

You can then access the data in your controller:

$extra = $form->get('extra')->getData();

UPDATE

The new way since Symfony 2.1 is to use the mapped option and set that to false.

->add('extra', null, array('mapped' => false))

Credits for the update info to Henrik Bjørnskov ( comment below )

于 2012-10-07T05:20:57.047 に答える
30

Since Symfony 2.1, use the mapped option:

$builder->add('extra', 'text', [
    'mapped' => false,
]);
于 2012-10-07T20:20:09.787 に答える
4

According to the Documentation:

allow_extra_fields

Usually, if you submit extra fields that aren't configured in your form, you'll get a "This form should not contain extra fields." validation error.

You can silence this validation error by enabling the allow_extra_fields option on the form.

mapped

If you wish the field to be ignored when reading or writing to the object, you can set the mapped option to false.

class YourOwnFormType extends AbstractType
{
    public function configureOptions(OptionsResolver $resolver)
    {
        $resolver->setDefaults(
            array(
                'allow_extra_fields' => true
            )
        );
    }

    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $form = $builder
            ->add('extra', TextType::class, array(
                'label' => 'Extra field'
                'mapped' => false
            ))
        ;
        return $form;
    }
}
于 2016-12-19T18:55:04.937 に答える