Symfony Choice type with disabled options

Flo Schild

Is there any way with Symfony to render a <select> form type with disabled options, based on the truthyness of the given choices options ?

I saw this thread (thanks to DonCallisto) about disabling choice expanded options ; However I do not want to have an expanded choice. I would like to keep a select element, with disabled options.

$builder->add('list', 'choice', array(
    'choices' => array(
        array(
            'value' => 1,
            'label' => '1',
            'disabled' => false
        ),
        array(
            'value' => 2,
            'label' => '2',
            'disabled' => false
        ),
        array(
            'value' => 3,
            'label' => '3',
            'disabled' => true
        )
    ),
    // Instead of
    // 'choices' => array(
    //     1 => 'Option 1',
    //     2 => 'Option 2',
    //     3 => 'Option 3'
    // )
);

# Which would render to the following element
<select [...]>
    <option value='1'>1</value>
    <option value='2'>2</value>
    <option value='3' disabled='disabled'>3</value>
</select>

I just can't find the way... Is it necessary to build its own field type ?

Jacer Omri

Since version 2.7, Symfony has introduced a way to set choice attributes using a callable, this is just what you need.

this code is taken from official Symfony documentation

$builder->add('attending', ChoiceType::class, array(
    'choices' => array(
        'Yes' => true,
        'No' => false,
        'Maybe' => null,
    ),
    'choices_as_values' => true,
    'choice_attr' => function($val, $key, $index) {
        // adds a class like attending_yes, attending_no, etc
        return ['class' => 'attending_'.strtolower($key)];
    },
));

you can use the 'choice_attr' and pass a function that will decide wether to add a disabled attribute or not depending on the value, key or index of the choice.

...
    'choice_attr' => function($key, $val, $index) {
        $disabled = false;

        // set disabled to true based on the value, key or index of the choice...

        return $disabled ? ['disabled' => 'disabled'] : [];
    },
...

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Symfony set value checked on a form type choice

From Dev

Symfony2 choice constraint/validation on entity field type

From Dev

Symfony 2 - add options into 'Choice' field after form is created

From Dev

Symfony choice field type reports "This value is not valid" when submitting an invalid option. How do I change this?

From Dev

Manipulate the list of countries provided by Symfony Intl and the form choice type

From Dev

Symfony2 disabled select

From Dev

Validation on dynamically generated options in choice field + Symfony2

From Dev

field array type in entity for form choice type field symfony

From Dev

Symfony 2.3 pass custom data to entity form, using choice or other type

From Dev

Symfony2: How to filter the options of an entity-choice form field by a certain attribute?

From Dev

Disable backend validation for choice field in Symfony 2 Type

From Dev

Pass custom value to each input in Symfony form (type is choice)

From Dev

Allow all value on choice field type in Symfony2 form builder

From Dev

How to render a multiple choice dropdown with option groups using "sonata_type_model" or symfony "entity"?

From Dev

Symfony Choice type with disabled options

From Dev

Symfony form choice disable translator

From Dev

Vuejs disabled selected dropdown options?

From Dev

Symfony3 : choice type field filled with array of objects

From Dev

Boolean values and choice symfony type

From Dev

Manipulate the list of countries provided by Symfony Intl and the form choice type

From Dev

Symfony2: what is a "norm" data for a choice field type in forms

From Dev

How to get the options from a symfony select type

From Dev

Rendering a choice field type (symfony2) as buttons

From Dev

Symfony2 / Twig - Label class for Expanded choice type

From Dev

Symfony creating choice from entity in form type

From Dev

Symfony form from child class does not load choice options

From Dev

Symfony form for a general entity with a type and different options

From Dev

Symfony 3 weird "choice" error

From Dev

Symfony Easyadmin choice type field for roles

Related Related

  1. 1

    Symfony set value checked on a form type choice

  2. 2

    Symfony2 choice constraint/validation on entity field type

  3. 3

    Symfony 2 - add options into 'Choice' field after form is created

  4. 4

    Symfony choice field type reports "This value is not valid" when submitting an invalid option. How do I change this?

  5. 5

    Manipulate the list of countries provided by Symfony Intl and the form choice type

  6. 6

    Symfony2 disabled select

  7. 7

    Validation on dynamically generated options in choice field + Symfony2

  8. 8

    field array type in entity for form choice type field symfony

  9. 9

    Symfony 2.3 pass custom data to entity form, using choice or other type

  10. 10

    Symfony2: How to filter the options of an entity-choice form field by a certain attribute?

  11. 11

    Disable backend validation for choice field in Symfony 2 Type

  12. 12

    Pass custom value to each input in Symfony form (type is choice)

  13. 13

    Allow all value on choice field type in Symfony2 form builder

  14. 14

    How to render a multiple choice dropdown with option groups using "sonata_type_model" or symfony "entity"?

  15. 15

    Symfony Choice type with disabled options

  16. 16

    Symfony form choice disable translator

  17. 17

    Vuejs disabled selected dropdown options?

  18. 18

    Symfony3 : choice type field filled with array of objects

  19. 19

    Boolean values and choice symfony type

  20. 20

    Manipulate the list of countries provided by Symfony Intl and the form choice type

  21. 21

    Symfony2: what is a "norm" data for a choice field type in forms

  22. 22

    How to get the options from a symfony select type

  23. 23

    Rendering a choice field type (symfony2) as buttons

  24. 24

    Symfony2 / Twig - Label class for Expanded choice type

  25. 25

    Symfony creating choice from entity in form type

  26. 26

    Symfony form from child class does not load choice options

  27. 27

    Symfony form for a general entity with a type and different options

  28. 28

    Symfony 3 weird "choice" error

  29. 29

    Symfony Easyadmin choice type field for roles

HotTag

Archive