You are here

function editableviews_entity_form_validate in Editable Views 7

Form validate handler.

Adapted from fape_field_edit_field_form_validate().

File

./editableviews.module, line 168
editableviews.module Contain module code. @todo:

Code

function editableviews_entity_form_validate($form, &$form_state) {
  ctools_include('fields');
  $field_handlers = $form['#field_handlers'];

  // Act on each entity in turn.
  foreach ($form['#entity_ids'] as $entity_type => $entity_ids) {
    foreach ($entity_ids as $entity_id) {
      $bundle = $form[$entity_type][$entity_id]['#bundle'];
      $entity = $form[$entity_type][$entity_id]['#entity'];
      $relationship = $form[$entity_type][$entity_id]['#views_relationship'];

      // Start with a fresh errors array for each entity. FieldAPI field
      // validation will add to this array.
      $errors = array();

      // Invoke the field handlers on the relationship the entity is on.
      foreach ($field_handlers[$relationship] as $field_handler) {
        $field_handler
          ->edit_form_validate($entity_type, $entity, $form[$entity_type][$entity_id], $form_state, $errors);
      }

      // Invoke hook_field_attach_validate() to let other modules validate the
      // entity. This should be done once only for each entity.
      // Avoid module_invoke_all() to let $errors be taken by reference.
      foreach (module_implements('field_attach_validate') as $module) {
        $function = $module . '_field_attach_validate';
        $function($entity_type, $entity, $errors);
      }
      if ($errors) {

        // Pass FieldAPI validation errors back to widgets for accurate error
        // flagging.
        foreach ($field_handlers[$relationship] as $field_handler) {
          $real_field_name = $field_handler->definition['field_name'];
          if (isset($errors[$real_field_name])) {
            $field_errors = array(
              $real_field_name => $errors[$real_field_name],
            );

            // This is ugly, but only FieldAPI field handlers need this. The
            // alternative would be for the editableviews_handler_field_field_edit
            // handler's edit_form_validate() method to detect whether it's the
            // last FieldAPI field handler for the current entity, which would be
            // messier.
            if (method_exists($field_handler, 'edit_form_validate_errors')) {
              $field_handler
                ->edit_form_validate_errors($entity_type, $entity, $form[$entity_type][$entity_id], $form_state, $field_errors);
            }
          }
        }
      }
    }
  }
}