You are here

function relation_add_field_required_validate in Relation add 7

It is called when the relation has required fields.

Parameters

array $element: Form element.

array $form_state: Form state value.

1 string reference to 'relation_add_field_required_validate'
relation_add_field_widget_form in ./relation_add.module
Implements hook_field_widget_form().

File

./relation_add.module, line 943
Relation Add module file.

Code

function relation_add_field_required_validate($element, &$form_state) {
  if (is_array($element)) {
    $form_path = implode('][', $element['#parents']) . '][relation_options][';
    $form_errors = form_get_errors();

    // Clear form errors.
    $drupal_errors = drupal_get_messages('error');
    form_clear_error();
    if (isset($drupal_errors['error']) && !empty($drupal_errors['error'])) {
      foreach ($drupal_errors['error'] as $key => $error) {
        if (in_array($error, $form_errors)) {

          // Unset form errors.
          unset($drupal_errors['error'][$key]);
        }
      }

      // Rebuild drupal errors.
      foreach ($drupal_errors['error'] as $message) {
        drupal_set_message($message, 'error');
      }
    }
    if (!empty($form_errors)) {
      foreach ($form_errors as $error_path => $error_msg) {
        if (strpos($error_path, $form_path) === 0) {
          $element_paths = explode('][', $error_path);
          $element_paths = array_slice($element_paths, count($element['#parents']));
          $empty_endpoints = TRUE;
          if ('targets' != $element_paths[1]) {

            // Find the field which has an error.
            $sub_element =& $element;
            foreach ($element_paths as $element_path) {
              if (isset($sub_element[$element_path])) {
                $sub_element =& $sub_element[$element_path];
              }
            }
            if (isset($sub_element['#required']) && $sub_element['#required']) {
              $required_msg = t('!name field is required.', array(
                '!name' => $sub_element['#title'],
              ));
              if ($required_msg == $error_msg) {
                $field_values = drupal_array_get_nested_value($form_state['values'], $element['#parents']);
                foreach ($field_values['relation_options']['targets'] as $target) {
                  if (!empty($target)) {
                    $empty_endpoints = FALSE;
                    break;
                  }
                }
              }
              if ($empty_endpoints) {
                continue;
              }
            }
          }
        }
        form_set_error($error_path, $error_msg);
      }
    }
  }
}