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.
\Drupal\Core\Entity\EntityInterface $entity: The entity being submitted. 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(EntityInterface $entity, $form, &$form_state, array $options = array()) {
// Ensure we are working with a BC mode entity.
$entity = $entity
->getBCEntity();
// Perform field_level validation.
try {
field_attach_validate($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);
}
}
$form_display = $form_state['form_display'];
field_invoke_method('flagErrors', _field_invoke_widget_target($form_display), $entity, $form, $form_state, $options);
}
}