Form bindings with copyFrom, copyTo and accept
Key Takeaway
Call copyFrom(obj) to prefill a form from an object. After the customer submits, call copyTo(obj) to write the submitted values back into that object.
Form bindings move data between an in-memory B2C Commerce form and a business object, such as a customer profile or a custom object. Three dw.web.FormGroup methods do the work. copyFrom() fills a form from an object. copyTo() and accept() write the submitted form values back into an object. Together they let a storefront form show stored data and then save the customer's edits.
FormGroup and form fields
dw.web.FormGroup is the central class of form handling and the container for fields and other form elements, including sub-forms. Form, FormList and FormListItem are all subclasses of FormGroup. You reach a field by name, for example myform.firstname.
In a form definition XML file, a field can carry a binding attribute next to its formid. For example, a contact form defines its name fields like this:
<field formid="firstname" ... binding="firstName"/>
<field formid="lastname" ... binding="lastName"/>Each field's binding value sits beside its formid: firstName for the firstname field and lastName for the lastname field.