symfonysymfony-forms

symfony form builder update option field


is it possible to update an option field after adding it ?

$builder
    ->add('examens', 'entity', array(
        'class' => 'TelegrammeExamenBundle:ExamExamen',
        'property'  => 'libelle',
        'required'    => true,
        'empty_value' => 'Sélectionnez un examen',
        //'data' => $this->em->getReference("TelegrammeExamenBundle:ExamExamen", 510),
        'data' => null,
        'query_builder' => function(ExamenRepository $r) {
            return $r->getSelectList();
        },
        'attr' => array('class' => 'bg_white_filet_gris')
    ))
;

how modify field option ??? (setOption don't exist)

if (...) $builder->get('examens')->setOption('property', 'test');

Solution

  • You can simply ->add() it again. As the API documentation suggests for the add method: Adds or replaces a child to the form

    http://api.symfony.com/2.8/Symfony/Component/Form/FormInterface.html#method_add

    This can be used to modify form elements for example in a FormEvent.


    Alternatively the FormBuilder provides a setAttribute() method which can be used as follows:

    $builder->get('examens')->setAttribute('property', 'test');