aem-core-wcm-components icon indicating copy to clipboard operation
aem-core-wcm-components copied to clipboard

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

Open HitmanInWis opened this issue 1 year ago • 1 comments

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 avatar Jul 03 '24 15:07 HitmanInWis

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.

HitmanInWis avatar Jul 03 '24 15:07 HitmanInWis