I try to do a contact form with Symfony 2.4.1 and I have 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 this error but I find anything to solve it in SF2 forms documentation or on the web :
The controller code is :
[..]
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 Entity is :
[...]
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 Form class code:
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', ));
}
}
Any idea of the problem ? Thanks
Your error is correct and telling you that you entity
Contact
does not have thecontact
property and no related getter setter method while in yourbuildForm()
you have used contact property like$builder->add('contact');
but there is no related property exists in the entity,Define the property first in your entityor if its a non mapped field then you have to define this field in builder as non mapped
By defining above you will not need to update your entity