Reputation: 1227
I have a form type which has a field that is not in the entity as a property, but the entity has a getter and a setter with the same name as form field, explaining:
Form type:
$builder->add('theField', 'entity', array(
'label' => 'The field',
'class' => 'MyAppBundle:AnEntity',
'empty_value' => '',
));
Entity:
class User
{
//There is NOT a property called "theField"
public function setTheField($value)
{
...
}
public function getTheField()
{
...
}
}
So, I'm expected that Symfony2 call the getter and setter to bind and show the form field, but I get this error:
Property theField does not exists in class My\AppBundle\Entity\User
Is there a way to create this form field without having the property declared in entity?
EDIT
Is strange, but when I declare a private property theField
, it's works (BTW, that is not what I looking for).
Upvotes: 2
Views: 3216
Reputation: 974
You can also do it with the mapped
option of symfony
$builder->add('chooseProduct', ChoiceType::class, array(
'mapped'=> false,
'required' => false,
'placeholder' => 'Choose',
'choices' => $this->entityManager->getRepository('App:Entity)->getSelectList(),
'label_attr' => array('class' => 'control-label')
));
Upvotes: 2
Reputation: 1702
Did you try:
$builder->add('theField', 'entity', array(
'label' => 'The field',
'class' => 'MyAppBundle:AnEntity',
'empty_value' => '',
'property_path' => false,
));
Change your field name to the same as attribute in the entity or change 'property_path'
to the attribute name.
$builder->add('theField', 'entity', array(
'label' => 'The field',
'class' => 'MyAppBundle:AnEntity',
'empty_value' => '',
'property_path' => 'theField',
));
And in your entity add:
private $theField = null;
Upvotes: 1