Problem/Motivation
A row whose only content is an unchecked checkbox is saved instead of being dropped. Every "Add another item" click the user then leaves alone becomes a stored empty row.
Steps to reproduce
Add a multi-value element with a checkbox child, leave a row's checkbox unchecked and its other children empty, and submit. The row is present in the submitted values.
Proposed resolution
isRowEmpty() decides whether a row is dropped, and treated only NULL and the empty string as empty. An unchecked checkbox submits integer 0, so the row never looked empty.
Integer 0 and FALSE now count as empty scalar values as well. The string '0' stays non-empty on purpose, since it is legitimate input for a textfield or number child, and there is a regression test for that case.
Remaining tasks
Review.
User interface changes
None.
API changes
A row containing only an unchecked checkbox is no longer saved. Code relying on those rows being present should add another child with content, or handle the row being absent.
Original report:
I added checkbox field into my multifield field.
Like this:
[
'#type' => 'checkbox',
'#title' => $this->t('Check it!'),
'#default_value' => FALSE,
]
Even checkbox is unchecked this multivalue element is saved.
Patch is coming.
Issue fork multivalue_form_element-3482809
Show commands
Start within a Git clone of the project using the version control instructions.
Or, if you do not have SSH keys set up on git.drupalcode.org:
Comments
Comment #5
super_romeo commentedComment #6
joevagyok commentedThanks for the fix, can we have some assertions for this please?
Comment #7
darvanenComment #9
darvanenReimplemented against 2.0.x with the test coverage that was asked for, in a fresh MR since the internals moved.
The cause: unchecked checkboxes submit integer 0, and
isRowEmpty()only treated NULL and the empty string as empty, so a row whose only content was an unchecked checkbox was kept. Integer 0 and FALSE now count as empty. The string '0' stays non-empty deliberately, since it is a legitimate textfield or number value, and there is a test pinning that so a later simplification cannot quietly break number fields.Tests cover:
Comment #10
darvanen