Ask Your Question
1

How can I remove items from the Symfony dropdown form type?

asked 2021-10-26 11:00:00 +0000

david gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
0

answered 2021-06-21 21:00:00 +0000

lalupa gravatar image

You can exclude specific choices from a Symfony dropdown form type by setting the "choices" option to an array of key-value pairs, where the keys are the values of the options and the values are the labels. Then, use the "choices_exclude" option to specify which values to exclude from the dropdown.

Example:

$form = $this->createFormBuilder()
    ->add('my_options', ChoiceType::class, [
        'choices' => [
            'Option 1' => 'option1',
            'Option 2' => 'option2',
            'Option 3' => 'option3',
            'Option 4' => 'option4',
        ],
        'choices_exclude' => ['option2', 'option3'],
    ])
    ->getForm();

In this example, the dropdown will show four options ("Option 1", "Option 2", "Option 3", and "Option 4"), but exclude "Option 2" and "Option 3".

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss

Add Answer


Question Tools

Stats

Asked: 2021-10-26 11:00:00 +0000

Seen: 13 times

Last updated: Jun 21 '21