During form validation, Salesforce B2C Commerce checks HTML form field values provided by the customer to ensure that they are valid. You can control how B2C Commerce performs form validation by modifying the corresponding form definition. The form definition determines which fields are validated and how they are validated. If a field value entered by the customer doesn't meet the constraints in the form definition (for example, the entered string is too short), B2C Commerce flags it as a form violation.
Form validation doesn't occur automatically, but is instead controlled by the form action that is fired. For example, the billing form definition contains a general save action:
<action formid="save" valid-form="true"/>
With valid-form
set to
true, the form is validated.
The type of validation that occurs depends on how you construct the form definition. There are two validation mechanisms: built-in and custom.
Custom Form Validation
You can define custom validation functions for
<field>
elements and form container elements, such
as <form>
, <group>
,
<list>
, and <include>
. If
you define a custom validation function for a form element, B2C Commerce
executes that function instead of applying built-in validation.
Custom Data
You can provide custom data during form validation in validation scripts. This data is looped through the validation process and is still accessible when rendering the validation results. This enables you to implement a more sophisticated feedback for the customer. For example, you might want to provide customers with detailed error messaging during address form group validation using data provided by an address validation service or even providing a did you mean alternative for incorrect entries.
This is a general example:
exports.validateMyGroup = function( group : FormGroup )
{
...
var data = new HashMap();
data.put("my_data_1", "some message");
data.put("my_data_2", 4711);
var formElementValidationResult = new
FormElementValidationResult(false, 'my.error', data);
...
return formElementValidationResult;
}
See the B2C Commerce API documentation on these classes:
Built-in Form Validation
Built-in form validation is based on attribute
settings for field definitions. For example, if an email field is
marked mandatory="true"
, B2C Commerce marks the field as
invalid if the customer doesn't submit a value. See Field Form Element.
When a customer submits a form, field values (strings) are sent electronically and then converted to the appropriate types as defined in the respective form definition.
When an action directs B2C Commerce to validate a form, B2C Commerce submits the form, validate the values entered and update the following flags:
htmlValue
property with the string representation of the formatted
value.null
.firedAction
and triggeredAction
properties.<
include>
) is invalid, the
surrounding form definition is also invalid.You can configure
custom validation functions not only for form fields
(<field>
) but also for form container elements
(<form>
, <group>
,
<list>
, and <include>
).
The first example shows a custom validation function
configured for a <field>
element:
<field formid="password"
label="label.password"
description="resource.customerpassword"
type="string"
mandatory="true"
range-error="resource.customerpassword"
validation="${require('~/cartridge/scripts/forms/my_custom_script.ds').my_custom_validation(formfield);}"
/>
The next example shows two custom validation functions
configured for two different container elements
(<form>
and
<group>)
:
<form xmlns="http://www.demandware.com/xml/form/2008-04-19"
validation="${require('~/cartridge/scripts/forms/MyCustomValidator.ds').validateThis(formgroup);}">
...
<group formid="myGroup"
validation="${require('~/cartridge/scripts/forms/MyCustomValidator.ds').validateThat(formgroup);}">
...
</group>
...
</form>
In both examples, the validation attribute
specifies the function to be called, and the system expects the validation
function to return an appropriate value. Validation functions can return a
boolean value or a dw.web.FormElementValidationResult
object.
A form
definition can specify multiple error messages for each form field (for
example, "value-error", "range-error", "missing-error" or "parse-error").
B2C Commerce validates the user entry and exposes one of these
error messages in the FormField.error
attribute,
depending on which validation condition was not
satisfied.
Starting in 15.8, it's possible to validate a field using a custom validation function. When you specify a custom validation function for a given field definition, you should only specify a range-error message. For more information, see Field Form Element.
The order of validation and corresponding error messages is as follows.
Field error attributes maintain an order of precedence, as follows:
The
FormElement.invalidateFormElement(error : String)
method
lets you specify an error message.
You can
specify a form-error
attribute in a form group. B2C Commerce sets "form-error" when the form group is invalidated
programmatically.
invalidateFormElement()
to a form group just sets the
error message. The isValid()
method continues to
return the validation status of the child elements.