4

I'd like to know if there's a way to set the value of an option in a select list. This select is a choice entity field.

Everything is working fine in my code. The point is that in my view I got the ID of each field in the value option and I need to have another field in there.

I'm using the option property to set what will be showed in the option name, but I need to set what will appear in the value field also.

I have no success to found a solution until now, so if anyone could help me out, it will be really appreciated.

A little part of my field in form type.

->add('fieldName','entity', array(
    'class'    => 'path\to\entity',
    'property' => 'name',
    'multiple' => true,
    'expanded' => false,
    )

Thanks.

My returned HTML code looks like this

<select>
    <option value="4">ROLE_EXAMPLE</option>
</select>

What I'm trying to do is get a result like this:

<select>
    <option value="specific_property_from_entity">ROLE_EXAMPLE</option>
</select>
grogers
  • 85
  • 1
  • 8
  • Can you explain what kind of values do you expect? – Bartek Jun 17 '14 at 12:58
  • Put here your generated hmtl and what you expect. – Sergio Costa Jun 17 '14 at 13:02
  • @bartek I've put an example above. Thanks. – grogers Jun 17 '14 at 13:08
  • @SergioCosta see example above. I've edited my question. Thanks – grogers Jun 17 '14 at 13:09
  • @grogers yep, I had the same problem and solved it by `choice` field type. – Bartek Jun 17 '14 at 13:10
  • I understand what you're trying to do but not why you need to do it? Assuming you are using symfony as intended then the select box returns the selected object from which you can easily retrieve whichever specific property you need to use. The select value is just used internally by Symfony to find the selected object. – redbirdo Jun 17 '14 at 13:29

3 Answers3

4

You can use choice type instead of entity, here's some example:

class YourType extends AbstractType
{
    protected $em;

    // Injecting EntityManager into YourType
    public function __construct(EntityManager $em)
    {
        $this->em = $em;
    }

    private function getChoices()
    {
        // some logic here using $this->em
        // it should return key-value array, example:
        return [
            'foo' => 'bar',
            'test' => 'abc', // and so on...
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
         $builder->add('fieldName', 'choice', [
            'choices' => $this->getChoices(),
             ]
         )
    }

}

You can read about creating field types as a service

Bartek
  • 1,349
  • 7
  • 13
0

If I understand your question, you are asking how to set a preferred choice on an entity field.

You can look at this answer: https://stackoverflow.com/a/12000289/3524372

Community
  • 1
  • 1
Einenlum
  • 600
  • 4
  • 15
0

You can pass choice_value option, which uses callable or property path (getProperty()) to populate your choice values

->add('fieldName','entity', array(
    'class'    => 'path\to\entity',
    'property' => 'name',
    'multiple' => true,
    'expanded' => false,
    'choice_value => 'Property'
)

If you are using a callable to populate choice_value, you need to check for the case that the value of the field may be null.

Akhmed
  • 1,141
  • 4
  • 21
  • 49