Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Validating symfony2 forms with extra fields

Tags:

symfony

I'm working on a symfony2 backend for a backbone.js application. I have my model and form.

However, backbone.js sends some additional properties to the REST API when it's creating/updating a model and I'm struggling to get the form to validate.

How can I get a form in symfony2 to accept additional data, or how can I drop particular keys before binding data to a form?

like image 965
user1432227 Avatar asked Jun 03 '12 08:06

user1432227


1 Answers

You should use option "allow_extra_fields".

public function setDefaultOptions(OptionsResolverInterface $resolver)
{
        $resolver->setDefaults(
        array(
            'allow_extra_fields' => true
        )
    );
}

For symfony 2.8+ use configureOptions(OptionsResolver $resolver) instead of setDefaultOptions(OptionsResolverInterface $resolver)

like image 66
krun Avatar answered Sep 23 '22 03:09

krun