I have an entity
form field in my Symfony2 project.
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('productId', 'genemu_jquerychosen_entity', array(
'class' => 'EMRSabaBundle:Product',
'property' => 'name'
))
;
}
The Product
entity has some objects like price, name, mode, & id I want to let user choose the product by name & see chosen produc's price, then submit product ID, not name.
Is there any soloution?
As per the documentation for this bundle:
You can use all the core choice types from Symfony (choice, country, ...) and Doctrine (ORM and ODM), you just have to prefix the type name with genemu_jqueryselect2_*
This means you are using a "regular" entity
field. You can do what you want by skipping the definition of property
and relying on the __toString()
method from EMRSabaBundle:Product
:
[property]
This is the property that should be used for displaying the entities as text in the HTML element. If left blank, the entity object will be cast into a string and so must have a __toString() method.
Change your code to:
$builder->add('productId', 'genemu_jquerychosen_entity', array(
'class' => 'EMRSabaBundle:Product'
))
And define the _toString()
method in your EMRSabaBundle:Product
object as follow:
public function __toString()
{
return $this->name . ' (' . $this->price . ')';
}
http://symfony.com/doc/current/reference/forms/types/entity.html#property
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.