adobe / aem-core-wcm-components

Standardized components to build websites with AEM.
https://docs.adobe.com/content/help/en/experience-manager-core-components/using/introduction.html
Apache License 2.0
747 stars 753 forks source link

Form Prefill restores default values for emtpy fields on form submit failure #2808

Open HitmanInWis opened 4 months ago

HitmanInWis commented 4 months ago

Bug present as of version: 2.24.7-SNAPSHOT

When a form submission fails, the Form Options field generally restores the options to what was submitted by the user. So if I have checkboxes submitted as:

[x] One [x] Two [ ] Three

On submit fail the values are restored to:

[x] One [x] Two [ ] Three

If one of the options is set to Selected as default by the field dialog the above still works unless the user submitted the form with all fields unchecked. So let's say "Three" is "Selected" by default.

User unchecks "Three" and submits

[ ] One [ ] Two [ ] Three

On failed submission, the form option is incorrectly reset to

[ ] One [ ] Two [x] Three

This issue also affects the Text field. This issue (default value restored for an empty submitted value) is not the same as https://github.com/adobe/aem-core-wcm-components/issues/2805 where Text field is giving priority to the default value over a non-empty submitted value.

HitmanInWis commented 4 months ago

To fix this one, we just need to update the logic on how prefill values are fetched in OptionItemImpl

Instead of:

String[] prefillValues = FormsHelper.getValues(request, options);

we can do this:

String[] prefillValues = FormsHelper.getValues(request, fieldResource);
if (prefillValues == null && ValidationInfo.getValidationInfo(request) != null) {
    prefillValues = new String[0];
}

Similar code updates would then be needed in TextImpl and HIddenImpl so adding a reusable util function for this would probably make most sense.