Symfony2 How to set a parameter value in a selection object field

I would like to know if there is a way to set the value of a parameter in a select list. This selection is a selection field.

Everything works fine in my code. The thing is, in my opinion, I got the identifier of each field in the value option, and I need to have another field.

I use the option property to indicate what will be shown in the parameter name, but I need to also set what will be displayed in the value field.

I have not managed to find a solution so far, so if someone can help me, he will be very appreciated.

A small part of my field is in the form type.

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

Thank.

My returned HTML is as follows

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

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

<select>
    <option value="specific_property_from_entity">ROLE_EXAMPLE</option>
</select>
+4
3

choice entity, :

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(),
             ]
         )
    }

}

+4

choice_value, callable property (getProperty())

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

choice_value, , null.

0

Source: https://habr.com/ru/post/1544906/


All Articles