Validation Result Presentation
The Validation Result Presenter Service is responsible for automatically displaying validation error messages in the DOM. It creates and manages error containers, making it easy to show validation feedback to users without manual DOM manipulation.
Overview
When validation errors occur, the ValidationResultPresenterService automatically:
Creates error message containers in the DOM
Populates containers with error messages
Removes error messages when validation passes
Manages the lifecycle of error elements
This service works behind the scenes as a subscriber to the validation controller, responding to validation events automatically.
How It Works
The presenter service uses special data attributes to identify and manage validation error containers:
validation-result-id: Identifies individual error message elementsvalidation-result-container: Marks the container that holds error messages
When a validation error occurs for a form element, the service:
Finds the parent element of the validated input
Looks for an existing
[validation-result-container]elementIf not found, creates a new
<div validation-result-container>elementAdds error messages as
<span validation-result-id="{id}">elements inside the container
Default Behavior
By default, when you use the & validate binding behavior, error messages are automatically displayed:
When validation fails, the DOM will automatically look like this:
Styling Error Containers
You can style the auto-generated error containers using CSS:
Pre-creating Error Containers
Instead of letting the service auto-create containers, you can pre-create them in your markup for better control:
DOM Structure
Understanding the generated DOM structure is important for styling and customization:
Using the Service Directly
You can inject and use the ValidationResultPresenterService directly for custom error presentation:
Service API
The ValidationResultPresenterService implements the following interface:
handleValidationEvent(event: ValidationEvent): void
Called automatically by the validation controller when validation results change. This is the primary integration point.
add(target: Element, results: ValidationResult[]): void
Manually add validation errors to a target element.
Parameters:
target: The DOM element associated with the validationresults: Array of ValidationResult objects to display
remove(target: Element, results: ValidationResult[]): void
Manually remove validation errors from a target element.
Parameters:
target: The DOM element to remove errors fromresults: Array of ValidationResult objects to remove
getValidationMessageContainer(target: Element): Element | null
Get or create the validation message container for a target element.
Parameters:
target: The validated input element
Returns: The container element, or null if the target has no parent
Behavior:
Searches for existing
[validation-result-container]in the parent elementIf not found, creates a new
<div validation-result-container>Appends the new container to the target's parent element
Custom Presenter Implementation
You can create a custom presenter by implementing the ValidationResultsSubscriber interface:
Register and use your custom presenter:
Integration with validation-errors Attribute
The presenter service works alongside the validation-errors custom attribute, which provides programmatic access to errors. See the Displaying Errors documentation for more information on using validation-errors.
Advanced Scenarios
Grouping Errors
Display all errors in a central location instead of next to each input:
Conditional Error Display
Show errors only after the user has attempted to submit:
Important Notes
The presenter service is automatically registered when you use
ValidationHtmlConfigurationError containers are created in the parent element of the validated input
Each error message has a unique
validation-result-idbased on the ValidationResult's internal IDThe service automatically handles adding and removing error messages as validation state changes
Empty error containers remain in the DOM after errors are cleared (they can be hidden with CSS)
See Also
Displaying Errors - Using the validation-errors attribute
Validation Controller - Managing validation lifecycle
Validate Binding Behavior - Marking inputs for validation
Last updated
Was this helpful?