Performs field validation against form-submitted field values.
There are two levels of validation for fields in forms: widget validation and and field validation.
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.
$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.
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);
}
}