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

Skip to content

[Form] Add ability to disable choice in form #7510

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 4 commits into from

Conversation

damour
Copy link
Contributor

@damour damour commented Mar 28, 2013

Example:

private $choiceList;

    public function __construct(EntityChoiceList $choiceList = null)
    {
        $this->choiceList = $choiceList;
    }

    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $options = array(
            'class' => 'AcmeDemoBundle:Category',
            'required' => true,
        );

        if ($this->choiceList) {
            $options['choice_list'] = $this->choiceList;
        }

        $builder->add('category', 'entity', $options);

    }

    public function finishView(FormView $view, FormInterface $form, array $options)
    {
       $ids = $this->getEmptyCategoryIds();

        foreach ($view->children['category']->vars['choices'] as $category) {
            if (in_array($category->value, $ids, true)) {
                $category->disabled = true;
            }
        }
    }

    private function getEmptyCategoryIds()
    {
        return ['2', '4'];
    }

* Creates a new ChoiceView.
*
* @param mixed $data The original choice.
* @param string $value The view representation of the choice.
* @param string $label The label displayed to humans.
* @param string $disabled Is choice disabled.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's a Boolean there

@stof
Copy link
Member

stof commented Mar 28, 2013

This should be supported in the PHP templates too, and should be tested

@marcospassos
Copy link

May be an option disabled_choices in the ChoiceType would be welcome. In this way it's not friendly.

// Array
$builder->add('category', 'entity', array('disabled_choices' => array($category));
// Closure
$ids = $this->getEmptyCategoryIds();
$disabledChoices = function($categories) use ($ids) {
    return $categories->filter(function ($category) {
      return in_array($category->value, $ids, true);
    });
};
$builder->add('category', 'choice', array('disabled_choices' => $disabledChoices));

@fabpot
Copy link
Member

fabpot commented Apr 20, 2013

ping @bschussek

@skrobek
Copy link

skrobek commented Jul 9, 2013

Any news ? Or any other merged PR with this functionality ?

@Barfly
Copy link

Barfly commented Sep 4, 2013

Same here, any news?
For now, i can't manage to disable specific options in a choice list (from an entity field)

@nurikabe
Copy link

+1

1 similar comment
@Miliooo
Copy link
Contributor

Miliooo commented Sep 20, 2013

+1

@cmodijk
Copy link

cmodijk commented Sep 30, 2013

Any news on this? I think this ticket could be closed with this? #3836

@webmozart
Copy link
Contributor

The implementation is fine, but missing a way to configure the "disabled" property, as @marcospassos pointed out. I think a nice API would be a "choice_attr" option which accepts a closure and receives each choice as argument:

$form->add('tags', 'choice', array(
    'choices' => // ...
    'choice_attr' => function ($choice) {
        if (/* ... */) {
            return array('disabled' => true);
        }

        return array();
    },
));

@webmozart
Copy link
Contributor

closely related to #4067

@stefancarlton
Copy link

+1

This would be really useful when working with expanded choices (e.g. checkboxes).

For example I am presently implementing a form which shows all possible facets for a search, as you filter the results some facets become unavailable - these need to be "disabled" but remain visible in the form.

@webmozart webmozart changed the title add ability to disable choice in form [Form] Add ability to disable choice in form Apr 8, 2014
@webmozart
Copy link
Contributor

Since there's no more work continuing on this PR, I'm closing it. I added the feature request to #4067.

@webmozart webmozart closed this Sep 25, 2014
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.