Processing a submitted form and returning field errors
Key Takeaway
Validation rules and error messages live in the form definition. When a field fails several checks, it gets one error. The parse and regex check (parse-error) runs before the mandatory and range checks, so a failure there wins.
When a shopper submits a storefront form, an SFRA controller route reads the submitted values and checks them against the rules in the form definition XML. The route then either saves the data or reports what is wrong. Because the validation rules and error messages live in the form definition, the server can reject bad input field by field. The shopper sees a specific, localized message for each problem.
Validation rules in the form definition
Server-side validation is configured in the form definition XML, not in controller code. Field attributes do the checking:
mandatoryrequires a value.regexpsets the allowed pattern.max-lengthsets the maximum length.
Length and pattern rules validate only string fields. max-length, min and max length, and regexp are used for validation only on type string. On other types, length values only help format the field. minValue and maxValue apply only to int, number and date fields.
For rules that attributes cannot express, use the validation attribute to name a script function. An example is validation="${require('~/cartridge/scripts/forms/my_custom_script.ds').my_custom_validation(formfield);}". You can run these validations on a container element, such as a form or a group, or on a single field.