Skip to content

Element Validator Service

Service identity

  • Service ID: name.element_validator
  • Interface: Drupal\name\Service\ElementValidatorInterface
  • Class: Drupal\name\Service\ElementValidatorService
  • Visibility: @internal — validation runs through the name form element; custom modules should use #type => 'name' and field widgets.

Validates minimum components, required state, and partial input on the Name form element. Works with Drupal\name\Utility\NameComponents for empty checks and missing-component detection.

Primary method

validate(array $element, FormStateInterface $form_state): array

Called from Name::validate(). When #needs_validation is set:

  • Enforces #minimum_components against filled values.
  • Applies #allow_family_or_given logic when configured.
  • Sets form errors on the element or individual components depending on whether inline_form_errors is enabled.

Returns the element array (possibly with #errors or child errors attached).

  • name.component_metadata — translated component labels used in error messages (NameComponentMetadataService).