Form SF2: Error Neither Property ... None of the Get Methods

I am trying to create a contact form in Symfony 2.4.1 and I get the following error:

Neither the property "contact" nor one of the methods "getContact()", "isContact()", "hasContact()", "__get()" exist and have public access in class "Open\OpcBundle\Entity\Contact". 

I understand the error itself, but I can’t find the resources to fix it in the documentation for SF2 forms or on the Internet:

The controller code is as follows:

 [..] class OpcController extends Controller { public function contactAction(Request $request) { $contact = new Contact(); $form = $this->createForm(new ContactType(), $contact); $form->handleRequest($request); return $this->render("OpenOpcBundle:Opc:contact.html.twig", array("formu" => $form->createView(), ) ); } } 

The contact person is as follows:

 [...] class Contact { protected $nom; protected $courriel; protected $sujet; protected $msg; public function getNom() { return $this->nom; } public function setNom($nom) { $this->nom = $nom; } public function getCourriel() { return $this->courriel; } public function setCourriel($courriel) { $this->courriel = $courriel; } public function getSujet() { return $this->sujet; } public function setSujet($sujet) { $this->sujet = $sujet; } public function getMsg() { return $this->msg; } public function setMsg($msg) { $this->msg = $msg; } } 

And the code of the Form class:

 public function buildForm(FormBuilderInterface $builder, array $options) { $builder->add('contact'); ->add('nom', 'text')) ->add('courriel', 'email') ->add('sujet', 'text') ->add('msg', 'textarea') ->add('submit', 'submit'); } public function getName() { return "Contact"; } public function setDefaultOptions(OptionsResolverInterface $resolver) { $resolver->setDefaults(array('data_class' => 'Open\OpcBundle\Entity\Contact', )); } } 

Where is my mistake? thanks

+4
source share
1 answer

Your error is correct and will tell you that the Contact object does not have the Contact property and no associated recipient configuration method, while in your buildForm() you used the contact property, for example $builder->add('contact'); but no property exists in the object. Define a property first in your entity.

 class Contact { protected $nom; protected $courriel; protected $sujet; protected $msg; protected $contact; public function getContact() { return $this->contact; } public function setContact($contact) { $this->contact= $contact; } /* ... remaining methods in entity */ } 

or if it is not a displayed field, then you must define this field in the builder as not displayed

 $builder->add('contact','text',array('mapped'=>false)); 

Having defined above, you will not need to update your entity

+21
source

Source: https://habr.com/ru/post/1439311/


All Articles