Symfony2 - Dynamic form choices - validation remove Symfony2 - Dynamic form choices - validation remove symfony symfony

Symfony2 - Dynamic form choices - validation remove


after much time messing around trying to find it. You basically need to add a PRE_BIND listener. You add some extra choices just before you bind the values ready for validation.

use Symfony\Component\Form\AbstractType;use Symfony\Component\Form\FormEvents;use Symfony\Component\Form\FormEvent;public function buildForm(FormBuilderInterface $builder, array $options){  // .. create form code at the top    $ff = $builder->getFormFactory();    // function to add 'template' choice field dynamically    $func = function (FormEvent $e) use ($ff) {      $data = $e->getData();      $form = $e->getForm();      if ($form->has('verified_city')) {        $form->remove('verified_city');      }      // this helps determine what the list of available cities are that we can use      if ($data instanceof  \Portal\PriceWatchBundle\Entity\PriceWatch) {        $country = ($data->getVerifiedCountry()) ? $data->getVerifiedCountry() : null;      }      else{        $country = $data['verified_country'];      }      // here u can populate choices in a manner u do it in loadChoices use your service in here      $choices = array('', '','Manchester' => 'Manchester', 'Leeds' => 'Leeds');      #if (/* some conditions etc */)      #{      #  $choices = array('3' => '3', '4' => '4');      #}      $form->add($ff->createNamed('verified_city', 'choice', null, compact('choices')));    };    // Register the function above as EventListener on PreSet and PreBind    // This is called when form first init - not needed in this example    #$builder->addEventListener(FormEvents::PRE_SET_DATA, $func);     // called just before validation     $builder->addEventListener(FormEvents::PRE_BIND, $func);  }


The validation is handled by the Validator component: http://symfony.com/doc/current/book/validation.html.

The required option in the Form layer is used to control the HTML5 required attribute, so it won't change anything for you, and that is normal.

What you should do here is to configure the Validation layer according to the documentation linked above.


Found a better solution which I posted here: Disable backend validation for choice field in Symfony 2 Type

Old answer:

Just spent a few hours dealing with that problem. This choice - type is really annoying. My solution is similar to yours, maybe a little shorter. Of course it's a hack but what can you do...

public function buildForm(FormBuilderInterface $builder, array $options){    $builder->add('place', 'choice'); //don't validate that    //... more form fields   //before submit remove the field and set the submitted choice as   //"static" choices to make "ChoiceToValueTransformer" happy   $builder->addEventListener(FormEvents::PRE_SUBMIT, function (FormEvent $event) {        $data = $event->getData();        $form = $event->getForm();        if ($form->has('place')) {            $form->remove('place');        }        $form->add('place', 'choice', array(            'choices' => array($data['place']=>'Whatever'),        ));    });}