Here is listing of my form:
$builder = $this->createFormBuilder($project)
->add('name','text')
->add('type','choice', array(
'choices' => $enumtype
))
->add('begindate','date')
->add('expecteddate','date')
->add('events', 'collection', array(
'type' => new EventType(),
'allow_add' => true,
'allow_delete' => true,
'by_reference' => false,
))
->add('financial', 'file', array(
'property_path' => false,
'required' => false
))
->add('investition', 'file', array(
'property_path' => false,
'required' => false
));
if ($defaults) {
$builder->add('id','hidden',array('data' => $defaults['id'], 'property_path' => false));
$form = $builder->getForm();
$form->setData($defaults);
}
else
$form = $builder->getForm();
When i try to validate this form, i receive FormError object:
Array (
[0] => Symfony\Component\Form\FormError Object (
[messageTemplate:protected] => This form should not contain extra fields.
[messageParameters:protected] => Array (
[{{ extra_fields }}] => id
)
[messagePluralization:protected] =>
)
)
If i exclude “id” field – all works ok.
How can i use hidden type and make validation?
This issue comes from the fact that the hidden parameter is optionnal.
A common mistake is not to set the associated type when submitting the form.
Example of mistake:
When calling the controller
ActuType()contains:When Submitting the form
ActuType()contains:They do not match.
This actually returns an error because there’s an extra field with hidden
idcontaining the row to edit when submitting the form.All you need to do is to check the request before initializing FormType
With this, you can set the same FormType that you did when asking for the page