简体   繁体   中英

Form shouldn't contain extra fields Symfony 4

I am trying to understand how Symfony works, so I tried to create two forms on the same page: one form for the firstname and the other for the email (I want two different forms so two different submit buttons).

I have the error This form should not contain extra fields. with Symfony 4 after I submit the form. The invalid value is the email.The violation message says " {{ extra_fields }}" => ""email" "

I read in the Symfony documentation that I have to enable the option allow_extra_fields .

So this is what I tried :

$emailForm = $this->createFormBuilder(['allow_extra_fields' => true])
                          ->add('email'  )
                          ->add('Submit',SubmitType::class)
                          ->getForm();

but it didn't work. How can I remove the error message ? There are a few previous posts about the error but the version 4 of Symfony seems to work differently.

This is a bit more of code :

       $userForm = $this->createFormBuilder($defaultData)
                    ->add('firstname', TextType::class)
                    ->add('email', EmailType::class)
                    ->add('description', TextType::class)
                    ->add('Submit', SubmitType::class)
                    ->getForm();

      $userForm->handleRequest($request);

       $defaultData = ['pass' => '', 'conf_pass' => ''];
       $passForm = $this->createFormBuilder($defaultData)
                ->add('pass', PasswordType::class, ['label' => 'Change password'])
                ->add('conf_pass', PasswordType::class, ['label' => 'Confirm password'])
                ->add('Submit', SubmitType::class)
                ->getForm();

      $passForm->handleRequest($request);

I think the problem comes from the fact that I am handling two times, but I'm not sure at all ...

My advice would be to actually use form classes . They are not hard to do, and they work very nicely overall. For example if you create a form type called UserType, the form fields will all be named like user[firstname] and so on. the handleRequest will also automatically pick them up and not stumble over the other forms potentially posted data. Then instead of creating a form builder in your controller, you call

$userForm = $this->createForm(UserType::class);
$userForm->handleRequest($request); // as before.

after that the if($userForm->isSubmitted() && $userForm->isValid()) check is very much enough to find out, which form was submitted.

(also, if you didn't know: you can only submit one form at a time anyway ;o)

However, I hope you're not doing too much custom form rendering. If you still would want to render your forms manually {{ userForm.firstname.vars.name }} should give you the form fields name (or maybe ...vars.id , i never can remember)

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