You are here

function content_multigroup_node_form_validate in Content Construction Kit (CCK) 6.3

Node form validation handler.

Perform validation for empty fields ignoring subgroups flagged for removal. Note that FormAPI validation for required fields is disabled because we need to accept empty fields that are flagged for removal.

1 string reference to 'content_multigroup_node_form_validate'
_content_multigroup_fieldgroup_form in modules/content_multigroup/content_multigroup.node_form.inc
Implementation of hook_fieldgroup_form().

File

modules/content_multigroup/content_multigroup.node_form.inc, line 382
Implementation of node edit functions for content multigroup.

Code

function content_multigroup_node_form_validate($form, &$form_state) {
  $type_name = $form['#node']->type;
  $groups = fieldgroup_groups($type_name);
  foreach ($form['#multigroups'] as $group_name => $group_fields) {
    $group = $groups[$group_name];
    $group_required = isset($group['settings']['multigroup']['required']) ? $group['settings']['multigroup']['required'] : 0;
    $non_empty_subgroups = $non_removed_subgroups = $required_field_errors = array();
    foreach ($group_fields as $field_name => $field) {

      // Tell the content module that it is not needed to enforce requirement
      // of fields in this multigroup because we are doing it here.
      // See content_multiple_value_nodeapi_validate().
      $form_state['values']['_content_ignore_required_fields'][$field_name] = TRUE;
      foreach ($form_state['values'][$field_name] as $delta => $item) {

        // Keep track of the highest delta value for this group.
        $max_delta = $delta;

        // Ignore subgroups flagged for removal.
        if ($form_state['multigroup_removed'][$group_name][$delta]) {
          continue;
        }

        // Keep track of non-removed subgroups.
        $non_removed_subgroups[$delta] = TRUE;
        $is_empty_function = $field['module'] . '_content_is_empty';
        if ($is_empty_function($form_state['values'][$field_name][$delta], $field)) {

          // Ignore fields that are not required.
          if (!$field['required']) {
            continue;
          }

          // Build an error message for this field in this subgroup, but do
          // not flag it, yet.
          if (!empty($item['_error_element'])) {

            // Here we don't know the number of elements and subelements a
            // widget could have added to the form, so we need to extract
            // components from the top, where we have group/delta/field, and
            // then push back field/delta on top of the list.
            $error_element = explode('][', $item['_error_element']);
            array_shift($error_element);
            array_shift($error_element);
            array_shift($error_element);
            array_unshift($error_element, $field_name, $delta);
            $error_element = implode('][', $error_element);
          }
          else {

            // Imagefield does not use error_element, sets error on the field.
            // Are there others that need different treatment?
            $error_element = $field_name;
          }
          $required_field_errors[$delta][$field_name] = array(
            'element' => $error_element,
            'message' => t('!name field is required in group @group.', array(
              '!name' => $form[$group_name][$delta][$field_name]['#title'],
              '@group' => t($group['label']),
            )),
          );
        }
        else {
          $non_empty_subgroups[$delta] = TRUE;
        }
      }
    }

    // Required multigroups require at least one non-empty subgroup of fields.
    if ($group_required && empty($non_empty_subgroups)) {
      form_set_error('', t('Group @name requires one collection of fields minimum.', array(
        '@name' => t($group['label']),
      )));
      continue;
    }

    // Force remove any empty groups so they will collapse.
    // Don't flag errors on empty groups.
    for ($delta = 0; $delta <= $max_delta; $delta++) {
      if (!isset($non_empty_subgroups[$delta]) && isset($non_removed_subgroups[$delta])) {
        unset($non_removed_subgroups[$delta]);
        $form_state['multigroup_removed'][$group_name][$delta] = TRUE;
        foreach ($group_fields as $field_name => $item) {
          $form_state['values'][$field_name][$delta]['_remove'] = TRUE;
        }
        if (isset($required_field_errors[$delta])) {
          unset($required_field_errors[$delta]);
        }
      }
    }

    // Ok, now we can flag errors for all required fields that have not been
    // filled in when they should.
    foreach ($required_field_errors as $delta => $error_list) {
      foreach ($error_list as $field_name => $error_info) {
        form_set_error($error_info['element'], $error_info['message']);
      }
    }
  }
}