Cert++
Close

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:

  • mandatory requires a value.
  • regexp sets the allowed pattern.
  • max-length sets 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.

B2C Commerce Developer Study Guide

Checklist progress

0/237Learned

Given a requirement to accept, validate, and persist information from a storefront customer, modify the appearance of a form, add validation and cross-site request forgery (CSRF) protection, and use bindings to process fields

0/9

Prepare for the Exam

Play Today's Certle

Study Community

Ask questions and get the latest info from other B2C Commerce Developer studiers. 796 members and growing.