0

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?

1 Answer 1

3

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.