Skip to content

[Form] ChoiceType children are always considered valid #60690

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

Open
ker0x opened this issue Jun 4, 2025 · 1 comment
Open

[Form] ChoiceType children are always considered valid #60690

ker0x opened this issue Jun 4, 2025 · 1 comment

Comments

@ker0x
Copy link
Contributor

ker0x commented Jun 4, 2025

Symfony version(s) affected

6.4.* / 7.*

Description

When a ChoiceType field with 'expanded' => true is submitted, all children are considered valid even if a selected value is invalid. Only the parent is considered invalid ('valid' => false in the form view data).

How to reproduce

Create a form with the following field:

->add('choice_checkbox', ChoiceType::class, [
    'choices' => [
        'Foo' => 'foo',
        'Bar' => 'bar',
        'Baz' => 'baz',
    ],
    'expanded' => true,
    'multiple' => true,
    'constraints' => new Assert\EqualTo('baz'),
])

Then select foo and/or bar and submit the form.

  • Parent field is invalid ('valid' => false)
  • Children are valid ('valid' => true) whereas foo and bar should be invalid ('valid' => false)

Image

reproducer

Possible Solution

No response

Additional Context

No response

@stof
Copy link
Member

stof commented Jun 4, 2025

this is expected, because the FormError is attached in the ChoiceType, not in its children.

The valid variable in form themes is defined as such:

  • if the form is not submitted, true (as we don't want to display the form as invalid if the user has not tried to submit yet)
  • otherwise, set it to false when that Form instance or any of its children contains some FormError (note that a disabled field is ignored in this, as such fields are ignored during submission as well).

As there is no FormError attached to the checkboxes, they have valid set to true

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants