function field_attach_form_validate

Performs field validation against form-submitted field values.

There are two levels of validation for fields in forms: widget validation and and field validation.

  • Widget validation steps are specific to a given widget's own form structure and UI metaphors. They are executed through FAPI's #element_validate property during normal form validation.
  • Field validation steps are common to a given field type, independently of the specific widget being used in a given form. They are defined in the field type's implementation of hook_field_validate().

This function performs field validation in the context of a form submission. It converts field validation errors into form errors on the correct form elements. Fieldable entity types should call this function during their own form validation function.

Parameters

$entity_type: The type of $entity; e.g. 'node' or 'user'.

Drupal\Core\Entity\EntityInterface $entity: The entity being submitted. The 'bundle', 'id' and (if applicable) 'revision' keys should be present. The actual field values will be read from $form_state['values'].

$form: The form structure where field elements are attached to. This might be a full form structure, or a sub-element of a larger form.

$form_state: An associative array containing the current state of the form.

array $options: An associative array of additional options. See field_invoke_method() for details.

Related topics

3 calls to field_attach_form_validate()
EntityFormController::validate in drupal/core/lib/Drupal/Core/Entity/EntityFormController.php
Implements Drupal\Core\Entity\EntityFormControllerInterface::validate().
EntityFormControllerNG::validate in drupal/core/lib/Drupal/Core/Entity/EntityFormControllerNG.php
Overrides EntityFormController::validate().
field_test_entity_nested_form_validate in drupal/core/modules/field/tests/modules/field_test/field_test.entity.inc
Validate handler for field_test_entity_nested_form().

File

drupal/core/modules/field/field.attach.inc, line 1107
Field attach API, allowing entities (nodes, users, ...) to be 'fieldable'.

Code

function field_attach_form_validate($entity_type, EntityInterface $entity, $form, &$form_state, array $options = array()) {

  // Perform field_level validation.
  try {
    field_attach_validate($entity_type, $entity, $options);
  } catch (FieldValidationException $e) {

    // Pass field-level validation errors back to widgets for accurate error
    // flagging.
    foreach ($e->errors as $field_name => $field_errors) {
      foreach ($field_errors as $langcode => $errors) {
        $field_state = field_form_get_state($form['#parents'], $field_name, $langcode, $form_state);
        $field_state['errors'] = $errors;
        field_form_set_state($form['#parents'], $field_name, $langcode, $form_state, $field_state);
      }
    }
    field_invoke_method('flagErrors', _field_invoke_widget_target(), $entity, $form, $form_state, $options);
  }
}