1

I have a form type in symfony2.5 that has choice type which has many options set dynamically as

public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder->add('service_id', 'choice', array(
        'label' => 'Service',
        'choices' => $this->getChoice()
    ));
    .......
    .......
}

protected function getChoice()
{
    $choices = array();

    $options = array(
        '1' => 'test1',
        .............
    );

    foreach($options as $key => $option)
    {
        if($key%2 == 0) {
            ....
        } else {
            ////disabled choice
        }
    }
}

How can i set this choices set disabled??

Bijesh Shrestha
  • 371
  • 2
  • 15

1 Answers1

1

This can be done using form event listeners and finishView method. Similar question has been answered before here - How to disable specific item in form choice type?

Note that PRE_BIND event is deprecated. Use PRE_SUBMIT instead.

Quote:

The events PRE_SUBMIT, SUBMIT and POST_SUBMIT were introduced in Symfony 2.3. Before, they were named PRE_BIND, BIND and POST_BIND.

http://symfony.com/doc/current/cookbook/form/dynamic_form_modification.html

Community
  • 1
  • 1
dmnptr
  • 4,258
  • 1
  • 20
  • 19