This is a common issue with custom JavaScript validation in AEC 2022 lead forms. The problem involves three interconnected aspects of the form validation system:
1. Custom JS Validation Logic
Your validation function structure is close but missing key components that the form framework needs. Here’s the correct format:
leadForm.addValidator('customBusinessRules', async (formData, context) => {
const errors = {};
if (formData.company && formData.company.length < 3) {
errors.company = 'Company name must be at least 3 characters';
}
return {
valid: Object.keys(errors).length === 0,
errors: errors,
fieldErrors: errors // Required for field-level error display
};
});
Key differences:
- Return both
errors and fieldErrors objects (they can be the same)
- Use field names as keys in the errors object
- The function should be async if you need to do any API calls or complex validation
- Include the
context parameter even if you don’t use it (required by the framework)
2. Form Submission Control
The form submission behavior is controlled separately from validation. You need to explicitly configure how the form handles validation failures:
leadForm.setSubmissionControl({
validateOnSubmit: true,
blockOnError: true,
showErrorSummary: true,
scrollToFirstError: true
});
Without this configuration, the form might block submission but won’t provide user feedback. The showErrorSummary option is particularly important - it displays a summary of all validation errors at the top of the form.
3. Error Message Display
This is the most commonly overlooked part. The form needs explicit configuration to know where and how to display custom validation errors:
Step 1: Register the validator with the error display system
leadForm.errorDisplay.register({
validatorName: 'customBusinessRules',
displayMode: 'inline', // Options: 'inline', 'summary', 'both'
errorClass: 'custom-validation-error',
clearOnInput: true
});
Step 2: Ensure your form template has error containers
Your HTML template must include error message placeholders for each field:
<div class="form-field">
<label for="company">Company Name</label>
<input type="text" id="company" name="company" />
<div class="field-error" data-field="company" role="alert"></div>
</div>
The data-field attribute must match the key you use in the validation errors object.
Step 3: Configure error message styling and behavior
leadForm.errorDisplay.configure({
showIcon: true,
animateIn: true,
errorIconClass: 'icon-error',
fieldHighlight: true,
highlightClass: 'field-invalid'
});
Complete Implementation Example:
// Initialize form with validation configuration
const leadForm = await leadSDK.forms.create({
formId: 'lead-capture',
validationMode: 'progressive', // Validates as user types
errorDisplayMode: 'both' // Shows both inline and summary errors
});
// Add custom validator
leadForm.addValidator('customBusinessRules', async (formData, context) => {
const errors = {};
// Company name validation
if (formData.company && formData.company.length < 3) {
errors.company = 'Company name must be at least 3 characters';
}
// Email domain validation
if (formData.email && formData.email.endsWith('@competitor.com')) {
errors.email = 'Please use a business email address';
}
return {
valid: Object.keys(errors).length === 0,
errors: errors,
fieldErrors: errors
};
});
// Register error display
leadForm.errorDisplay.register({
validatorName: 'customBusinessRules',
displayMode: 'both',
errorClass: 'custom-validation-error',
clearOnInput: true
});
// Configure submission behavior
leadForm.setSubmissionControl({
validateOnSubmit: true,
blockOnError: true,
showErrorSummary: true,
scrollToFirstError: true,
disableSubmitOnError: true
});
// Handle validation events
leadForm.on('validation:failed', (event) => {
console.log('Validation failed:', event.errors);
// Optional: Send analytics event or show notification
});
Troubleshooting Steps:
- Enable form debug mode: `leadForm.setDebugMode(true)
- Check console for validation events: Look for
[Form] Validation executed messages
- Verify error containers exist:
document.querySelectorAll('[data-field]') should return elements
- Test error display manually: `leadForm.errorDisplay.show(‘company’, ‘Test error message’)
- Check CSS: Ensure
.field-error class is visible (not display: none)
If errors still don’t display after these changes, check that your form template is using the correct AEC 2022 form structure. Older form templates from AEC 2021 might not have the required error container elements. You can verify this by inspecting the form HTML and confirming that each input field has a corresponding error container with the data-field attribute.
This draft is based on general Adobe Experience Cloud knowledge. It has not been verified against your specific version and environment. Practitioners: verify the steps and share your experience below.