简体   繁体   中英

Symfony2 Extra fields FormError on validating form with hidden field

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:

public function addOrEditAction($id=null)
{
    $request = $this->getRequest();

    if (!$id) {
        $model = new Actu();
        $type  = new ActuType(); /* I do not set the default id on submit */
    } else {
        $em = $this->getDoctrine()->getEntityManager();
        $model = $em->getRepository("MyBundle:Actu")
                    ->find($id);
        if (!$model) {
            return $this->redirect($this->generateUrl('admAddNew'));
        } else {
            $type = new ActuType($model->getId());
        }
    }

    $form = $this->createForm($type,$model);

    if ('POST' == $request->getMethod()) {
        $form->bind($request);
        if ($form->isValid()) {
            $em = $this->getDoctrine()->getEntityManager();
            $em->persist($model);
            $em->flush();

            return $this->redirect($this->generateUrl('admNews'));
        }
    }

    $data = array('form'=>$form->createView());
    return $this->render('MyBundle:Page:news-add.html.twig',$data);
}

When calling the controller
ActuType() contains:

'name', 'content', 'date', 'id'

When Submitting the form
ActuType() contains:

'name', 'content', 'date'

They do not match.
This actually returns an error because there's an extra field with hidden id containing the row to edit when submitting the form.

All you need to do is to check the request before initializing FormType

if (!$id && null === $id = $request->request->get('newsType[id]',null,true)) {

With this, you can set the same FormType that you did when asking for the page

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM