Form Definition Elements

Form definitions are XML files that define to Salesforce B2C Commerce how browser input and action fields should behave. A form element <form> is a top level group of fields, represented by the dw.web.Form API class.

Attribute Description
error Error text, which can be shown if the form is invalid. Show the error using the .properties techniques described earlier.
formid

Derived from the file name of the form definition.

Don't modify this value. It's system generated.

form-error The error message to use for form container. If the container is considered invalid, this is the error message that is set on the container. See the pipelet invalidateFormElement.
object-def The value of object-def must be the name of a business object, for example, CustomerAddress, or the name of custom business object that is prefixed with Custom (for example, Custom.MyObject).
secure Boolean value indicating whether or not the form should use the Secure Form Framework (SFF). SFF has been deprecated; leave this value at its default of false. Use Cross Site Request Forgery Protection (CSRF) instead.
validation B2C Commerce script expression that resolves to a custom validation function provided by a B2C Commerce Script Module. The referenced function can return a dw.web.FormElementValidationResult object. The form-error attribute specifies a generic error message that is used whenever your validation function returns false (that is, if the FormElementValidationResult.isValid() method evaluates to false). The validation attribute was added in version 16.1.

A form definition can contain one or more elements:

Example one: built-in validation

This form definition references the system object CustomerAddress, and for each CustomerAddress, the firstName, lastName, address1, and address2.

<form object-def="CustomerAddress">
       <field attribute-def="firstName"/>
       <field attribute-def="lastName" label="forms.orderaddress.003"/><!-- overwrite label -->
       <field attribute-def="address1" mandatory="false"/> <!-- overwrite mandatory flag -->
       <field attribute-def="address2"/>
</form>

Example two: custom validation function

This form definition specifies the validation attribute on the form element:

<form xmlns="http://www.demandware.com/xml/form/2008-04-19"
      validation="${require('~/cartridge/scripts/forms/CustomerAddressValidator.ds').validateCustomerAddress(formgroup);}"
      form-error="customeraddress.validation.general.error"
>
...
</form>

The custom validation function might look something like this:

importPackage(dw.web);

exports.validateCustomerAddress = function(customerAddressForm: FormGroup) {

    // Add your custom validation logic here. Make sure you 
    // construct a FormElementValidationResult object as 
    // your return value.

    return formElementValidationResult;
}