Configuring Form Validation Error Tracking
Our form validation error reporting allows to you see which form validation errors are being seen by users most often over a particular time period. You can access this by going to Analyze > Technical > Form Errors from the navigation bar.
There are two ways to configure form validation error reporting, both of which are summarised below. The first method is using inline error tagging, which requires the addition of attributes within the HTML of your website where form errors are triggered, while the second method requires the creation of a field error callback, which needs to be implemented within the DXA reporting application.
Inline error tagging
Inline Error Tagging allows you to add attributes to the relevant fields or error messages to tag form validation errors and share these with DXA. This can be achieved using the data-di-field-error and/or data-di-field-error-for.
<input type="text" id="FirstName" data-di-field-error="Please enter your first name.">
This will return "Please enter your first name" to the Form Validation Error Report.<label>
First name: <input type="text" id="first_name" data-di-field-error>
<span class="error" data-di-field-error-for="first_name">First name must be provided</span>
</label>
Field error callback
The field error callback requires custom configuration in the form of a JavaScript function. This should be configured to ensure that the relevant errors of fields within a form are returned to DXA. This needs to be added to the Field Error Callback field in Form Settings.
<form action="submit.php" method="POST" id="contact">
<label>
First name: <input type="text" name="first_name">
<span class="error">First name must be provided</span>
</label>
<label>
Last name: <input type="text" name="last_name" />
</label>
<input type="submit" value="Submit" />
</form>
While the below code shows how a field error callback would be written for this, along with comments to explain how this has been written:function (field) {
// Locate the error element by finding the first sibling with a "error" class
var error = field.parentNode ? field.parentNode.querySelector('.error') : false;
// If no error element exists, end the function by returning false
if(!error) return false;
// If an error exists and has text, trim the text and return the message
return error.innerText ? error.innerText.trim() : false;
}