I created a form in symfony2, the form has a one to Many relationship.
when I try to print the form in the template, it says:
Fatal error: Call to undefined method X\XBundle\Entity\RepresentanteLegal::getName() in
C:\xampp\htdocs\X\vendor\symfony\src\Symfony\Component\Form\FormFactory.php on line 192
I don’t have a getName() function on RepresentanteLegal because I don’t have a Name attribute.
So, I need some help.
I have generated a form in the project and I didn’t have any trouble.
This is RepresentanteLegalType:
namespace Sofla\SoflaBundle\Form;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilder;
class RepresentanteLegalType extends AbstractType
{
public function buildForm(FormBuilder $builder, array $options)
{
$builder
->add('nombre')
->add('apellido')
->add('telefonoCelular')
->add('telefonoLocal')
;
}
public function getName()
{
return 'sofla_soflabundle_representantelegaltype';
}
}
and this is the construct of RepresentanteLegal.php
public function __construct()
{
$this->empresa = new \Doctrine\Common\Collections\ArrayCollection();
}
Also, in RepresentanteLegal.php there are attributes, getters and setters but I won’t post them here because I believe that is not relevant.
EDIT: I found the solution, it was because I was calling:
$form = $this->createForm(new RepresentanteLegal(), $hojadevida);
and not
$form = $this->createForm(new RepresentanteLegalType(), $hojadevida);
I’m so sorry. My mistake…
You should supply your form with default form name. Create function in your form class