Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Symfony3 allow_extra_fields

Tags:

forms

symfony

I'm trying to add two extra fields to a form:

$this->contactForm = $this->createFormBuilder($contact, array('allow_extra_fields' =>true))
->add('Nom',        TextType::class)
->add('Prenom',     TextType::class)
->add('Telephone',  TextType::class, array(
    'label' => 'Téléphone'))
->add('Email',      TextType::class)
->add('Ajouter',    SubmitType::class)
->getForm();

But I get this error:

Neither the property "Nom" nor one of the methods "getNom()", "nom()", "isNom()", "hasNom()", "__get()" exist and have public access in class "CommonBundle\Entity\Contact".

How can I prevent this error?

like image 479
Valentin BEAULE Avatar asked Oct 18 '25 19:10

Valentin BEAULE


1 Answers

allow_extra_fields is for when a form is submitted, that it will allow fields not specified in your form to also be passed for example in your form, if when it was submitted it contained a field for 'foobar' it would not throw an error saying "form should not contain extra fields".

Since 'Nom' in not mapped in your entity, you need to specify that the field is not mapped. See http://symfony.com/doc/current/reference/forms/types/form.html#mapped for info

$this->contactForm = $this->createFormBuilder($contact, array('allow_extra_fields' =>true))
->add('Nom',        TextType::class, array('mapped'=>false))
->add('Prenom',     TextType::class)
->add('Telephone',  TextType::class, array(
    'label' => 'Téléphone'))
->add('Email',      TextType::class)
->add('Ajouter',    SubmitType::class)
->getForm();
like image 118
Derick F Avatar answered Oct 21 '25 13:10

Derick F