Symfony: Allowing extra field in validated forms
A quick hint: it unnerves me when I have to add a new field to a Doctrine form that does not belong to the model and thus always fires a validator error. Since I don’t have time and don’t want to create a whole validator array for my rather big form, I used
$this->validatorSchema->setOption('allow_extra_fields', true);
to allow extra fields in my form. Use only if you do validation otherwise or if the fields you add aren’t critical if they aren’t validated (e.g. in my case a <select>)
EDIT: it seems that when using the code above, the added form fields are not added to $form (or, in fact, not sent back to the action). as soon as I figure out a fix, I’ll update this post.
EDIT: i’ve found the source of the problem: allow_extra_fields allows for adding the fields, but Doctrine filters them before processing the $form item, so we have to add:
$this->validatorSchema->setOption('filter_extra_fields', false);
to make it work, since only in this manner the filtering is disabled and the data is submitted.
