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 elements

  • validation-result-container: Marks the container that holds error messages

When a validation error occurs for a form element, the service:

  1. Finds the parent element of the validated input

  2. Looks for an existing [validation-result-container] element

  3. If not found, creates a new <div validation-result-container> element

  4. Adds 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 validation

  • results: 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 from

  • results: 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 element

  • If 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 ValidationHtmlConfiguration

  • Error containers are created in the parent element of the validated input

  • Each error message has a unique validation-result-id based on the ValidationResult's internal ID

  • The 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

Last updated

Was this helpful?