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?
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();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With