Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[Form] Implemented attributes for choices #10309

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from

Conversation

florianv
Copy link
Contributor

Q A
Bug fix? no
New feature? yes
BC breaks? no
Deprecations? no
Tests pass? yes
Fixed tickets
License MIT
Doc PR

Use cases

Add attributes to choices like it's possible with any other element for example to add microdata attributes to use in select2 templates or disable choices.

Usage

There are a few ways to apply attributes to choices:

By specifying the choices_attr option

Note: It works only when using choices. See below for when using choice_list.

As an array

$builder->add('choice', 'choice', null, array(
    'choices' => array('a' => 'A', 'b' => 'B', 'c' => 'C'),
    'choices_attr' => array(
        'a' => array('disabled' => true, 'data-foo' => 'bar'),
        'c' => array('disabled' => true, 'data-foo' => 'bar'),
    ),
));

As a callable

$builder->add('choice', 'choice', null, array(
    'choices' => array('a' => 'A', 'b' => 'B', 'c' => 'C'),
    'choices_attr' => function ($choice) {
        $attributes = array();

        if ('A' === $choice || 'C' === $choice) {
            $attributes['disabled'] = true;
            $attributes['data-foo'] = 'bar';
        }

        return $attributes;    
    }
));

By passing the attributes to the choice lists

$builder->add('choice', 'choice', null, array(
    'choice_list' => new SimpleChoiceList(
        array('a' => 'A', 'b' => 'B', 'c' => 'C'), array(), array(
            'a' => array('disabled' => true, 'data-foo' => 'bar'),
            'c' => array('disabled' => true, 'data-bar' => 'baz'),
        )
    )
);

Edit : It is related to the following issues

#7510
#9694
#3836

@jakzal jakzal added the Form label Feb 22, 2014
@stof
Copy link
Member

stof commented Mar 4, 2014

it should be possible to build the attribute with a callable instead of having to pass the attributes for each choice directly IMO

@florianv
Copy link
Contributor Author

I added this feature. It was also mentionned in this comment #7510 (comment)

@neutral-id
Copy link

Will this be available soon?

@mayeco
Copy link
Contributor

mayeco commented Apr 9, 2014

@fabpot Any news about this??

@malarzm
Copy link
Contributor

malarzm commented May 23, 2014

👍 just needed this :)

@mayeco
Copy link
Contributor

mayeco commented May 29, 2014

👍

@piotrantosik
Copy link
Contributor

👍

2 similar comments
@daum
Copy link
Contributor

daum commented Jun 19, 2014

+1

@ppawlakov
Copy link

+1

@Koc
Copy link
Contributor

Koc commented Sep 5, 2014

@webmozart can you merge this PR?

@shokora
Copy link

shokora commented Sep 10, 2014

+1

@eko
Copy link
Contributor

eko commented Sep 22, 2014

+1 for this, would be helpful :)

@CSchulz
Copy link

CSchulz commented Sep 22, 2014

I think it also relates to #9289.

👍

Any chance to support this PR?

@arkste
Copy link
Contributor

arkste commented Sep 26, 2014

👍

@webmozart
Copy link
Contributor

Replaced by #12148.

@webmozart webmozart closed this Oct 6, 2014
@florianv florianv deleted the choices branch February 27, 2015 19:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.