Skip to content

[LiveComponent] Fix data-model on radio/checkbox inside Twig Components#3680

Open
Amoifr wants to merge 1 commit into
symfony:3.xfrom
Amoifr:fix-3412-data-model-radio-checkbox
Open

[LiveComponent] Fix data-model on radio/checkbox inside Twig Components#3680
Amoifr wants to merge 1 commit into
symfony:3.xfrom
Amoifr:fix-3412-data-model-radio-checkbox

Conversation

@Amoifr

@Amoifr Amoifr commented Jun 12, 2026

Copy link
Copy Markdown
Contributor
Q A
Bug fix? yes
New feature? no
Issues Fix #3412
License MIT

Supports the approach discussed in #3412.

data-model on a Twig Component wrapping <input type="radio"> / <input type="checkbox"> currently overwrites the per-input value attribute with the current prop value (so every input in a group ends up indistinguishable), and data-model="selected[]" crashes the PropertyAccessor.

DataModelPropsSubscriber now:

  • Bug 2 — strips the JS-only [] notation (rtrim($parent, '[]')) before resolving the property path.
  • Bug 1 — for radio/checkbox, computes checked instead of overwriting value. Detection does not rely on a type prop (which may be hardcoded in the child template, e.g. the Shadcn Checkbox@Kocal's concern), but on the prop value and the presence of an explicit value:
    • bool prop → boolean checkbox → set checked;
    • explicit value + array prop → checkbox group → checked = in_array(value, prop), keep value;
    • explicit value + scalar prop → radio → checked = (value == prop), keep value;
    • otherwise (text/email/number/…) → unchanged, value is set from the prop.

Comparisons are kept loose to mirror the JS setValueOnElement behavior. Tests cover radio, checkbox group (incl. the [] notation), a boolean checkbox whose type is hardcoded in the template, and a regular text input (no regression).

One edge case worth confirming: a text input carrying both an explicit value and a data-model is now treated as radio/checkbox. This is contradictory usage that is already broken today, so there should be no regression — but happy to adjust if you'd prefer a stricter detection.

@carsonbot carsonbot added Bug Bug Fix LiveComponent Status: Needs Review Needs to be reviewed labels Jun 12, 2026
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.

[LiveComponent] data-model on radio and checkbox inside a Twig Component overwrites all value attributes with the current prop value

2 participants