User tests: Successful: Unsuccessful:
When a row is added to a subform, the fields of the rows that a nested subform already has (its minimum or default rows) kept the X placeholder of the row template in their names and ids. The values of those rows were submitted under the placeholder key, so they were lost or ended up in another row on save.
Pull Request resolves #48606.
When a row is added to a subform that contains a nested subform, the nested subform's own existing rows (its min rows or default rows) are not renamed. Their fields keep the X placeholder of the row template in their names, ids and labels, for example jform[params][items][itemsX][children][children0][child].
On save, the values of those nested rows are submitted under the placeholder key itemsX instead of the key of their row. With more than one new row they overwrite each other, the data ends up in the wrong row or is lost, and an extra empty row appears after reload. The ids are duplicated across rows as well.
Each subform instance fixes only its own fields. fixUniqueAttributes() of the outer subform skips the fields inside a nested subform and only renames the nested joomla-field-subform element. The name setter of the nested subform updated its row template, but not the rows it already has.
The name setter now also renames the fields of the existing rows (fixNamePrefix()). Only the prefix of the name, id, for and aria-describedby attributes is replaced. The nested subform's own row index is in a later part of the name and is not affected. A nested subform further down is renamed the same way and its own setter fixes its rows, so deeper nesting works recursively.
Two smaller changes in the same area:
name, id, for and aria-describedby attributes in the same way as the rows, instead of only name=". Without that, rows added later from a nested template kept stale ids from the third level down, and name based ids like the -rules element of the password field and an aria-describedby with several ids were missed. The id is derived from the name like FormField::getId() does, so a subform name with a hyphen works too.fixUniqueAttributes() looks up the label by the id derived from the name when the element has no id. The joomla-field-subform element has no id, so the label of a nested subform field in a new row was never renamed.The PR changes JavaScript. Use the prebuilt package of this PR, or apply it and run npm ci.
Core has no form with a nested subform, so add one temporarily. Open modules/mod_custom/mod_custom.xml and add this field inside <fields name="params"> / <fieldset name="options">:
<field name="items" type="subform" label="Items" multiple="true">
<form>
<field name="name" type="text" label="Name" />
<field name="flag" type="checkbox" label="Flag" value="1" />
<field name="kind" type="radio" label="Kind" default="a" class="btn-group">
<option value="a">A</option>
<option value="b">B</option>
</field>
<field name="children" type="subform" label="Children" multiple="true" min="1" layout="joomla.form.field.subform.repeatable-table">
<form>
<field name="child" type="text" label="Child" />
<field name="color" type="list" label="Color" default="red">
<option value="red">Red</option>
<option value="green">Green</option>
<option value="blue">Blue</option>
</field>
<field name="tags" type="checkboxes" label="Tags">
<option value="x">X</option>
<option value="y">Y</option>
<option value="z">Z</option>
</field>
<field name="active" type="checkbox" label="Active" value="1" />
<field name="size" type="radio" label="Size" default="s" class="btn-group">
<option value="s">S</option>
<option value="l">L</option>
</field>
</form>
</field>
</form>
</field>Click the labels of the checkboxes and radio buttons rather than the inputs, so the for attributes are tested too.
Remove the field from mod_custom.xml again when done.
After saving, three rows are shown: "Outer 1" with an empty nested row, an empty row with the nested rows "Child 2" and "Child 2b", and "Outer 2" with an empty nested row. "Child 1" and its options are lost.
Before saving, clicking a label in a nested row of the second outer row ticks the input in the first outer row, because the ids are the same in both rows. In the browser inspector the inputs of the nested rows have names containing [itemsX].
After saving, the two rows are shown with their own values: "Outer 1" / "Child 1" and "Outer 2" / "Child 2", "Child 2b", with the options as entered.
The inputs of the nested rows have the name of their row, [items0] and [items1], and unique ids. The labels toggle the input of their own row.
Please select:
Documentation link for guide.joomla.org:
No documentation changes for guide.joomla.org needed
Pull Request link for manual.joomla.org:
No documentation changes for manual.joomla.org needed
| Status | New | ⇒ | Pending |
| Category | ⇒ | JavaScript NPM Change |
I have tested this item ✅ successfully on 851593b
I have tested this item ✅ successfully on 851593b
I am sorry, the fix is incorrect.
Each Subform instance should fix only their own fields, or we will get much fancier bugs.
Previously, the code filtered out fields belonging to nested subforms. Removing that filtering means the outer subform's fixUniqueAttributes() can rewrite names and IDs throughout the nested subtree, even though those nested rows have their own indexing and naming rules.
A tip, following @Fedik's point: the nested subform can do this itself in its name setter. The setter already renames the row template when the outer row is added; the rows that already exist in the nested subform (min / default rows) are the only thing it leaves out.
yes thats how i am rewriting it. thanks stefan and Fedik for your feedback.
| Labels |
Added:
NPM Resource Changed
PR-6.2-dev
|
||
Thanks @Fedik, you are right. I reworked it: the "Filter out nested" block is back unchanged, and the outer subform only renames the nested joomla-field-subform element, as before. The name setter of the nested subform now fixes the fields of its own existing rows, in addition to its row template. Deeper levels are reached the same way through their own setter, so each instance only touches its own fields. The description is updated with the details.
@tecpromotion the code changed since your test, could you test again?
Looks okay.
Would be good to have extensive test. For subform with few more fields, checkboxes, radio and select elements.
Looks okay.
Would be good to have extensive test. For subform with few more fields, checkboxes, radio and select elements.
Thanks @Fedik. I tested with a wider form: the outer rows with text, checkbox and radio fields, the nested rows with text, list, checkboxes, checkbox and radio fields, two new outer rows and an extra nested row added in the second one, and all options set by clicking their labels. Names, ids and labels are correct before saving and all values are saved in the right rows. The testing instructions now use that form, so testers cover these field types too.
This might have been fixed with my pr #48599