You are here

function conditional_fields_node_form_validate in Conditional Fields 6.2

Validation for node editing form.

1 string reference to 'conditional_fields_node_form_validate'
conditional_fields_node_after_build in ./conditional_fields.module
Main tasks:

File

./conditional_fields.module, line 897
Content fields and groups visibility based on the values of user defined 'trigger' fields.

Code

function conditional_fields_node_form_validate($form, &$form_state) {

  // When form fails validation, hook_form_alter is not called, so we add js here too
  if ($form['#conditional_fields']['settings']['ui_settings']) {
    conditional_fields_add_js($form['#conditional_fields']['settings']);
  }

  // Rebuild the array grouping the data by controlled fields
  // Needed to handle multiple controlling field per controlled field
  $controlled_fields = array();
  foreach ($form['#conditional_fields']['data'] as $row) {
    $controlled_fields[$row['field_name']][$row['control_field_name']] = $row['trigger_values'];
  }
  foreach ($controlled_fields as $controlled_field_name => $controlling_fields) {

    // Check if all controlling field were triggered
    $triggered = FALSE;
    foreach ($controlling_fields as $controlling_field_name => $trigger_values) {
      $triggered = conditional_fields_is_triggered($form_state['values'][$controlling_field_name], $trigger_values);
      if ($triggered == FALSE) {
        break;
      }
    }
    $required_fields = $form['#conditional_fields']['required_fields'];
    if (!empty($required_fields[$controlled_field_name]['in_group'])) {
      $controlled_field =& $form[$required_fields[$controlled_field_name]['in_group']][$controlled_field_name];
    }
    else {
      $controlled_field =& $form[$controlled_field_name];
    }

    // Controlled field
    if (strpos($controlled_field_name, 'field_') === 0) {
      if ($triggered) {

        // Check required
        if (!empty($required_fields) && $required_fields[$controlled_field_name]) {
          $in_group = $required_fields[$controlled_field_name]['in_group'];

          // Check if the controlled field is empty
          if (conditional_fields_is_empty($form_state['values'][$controlled_field_name], $in_group ? $form[$in_group][$controlled_field_name] : $form[$controlled_field_name], $form['#field_info'][$controlled_field_name])) {

            // Check whether the controlled field is in a group or not and set error accordingly
            if (!$in_group) {
              form_error($controlled_field, t('!name field is required.', array(
                '!name' => $controlled_field['#title'],
              )));
            }
            else {

              // Set error only if the containing group is not controlled or is controlled and triggered
              $set_error = TRUE;
              foreach ($form['#conditional_fields']['data'] as $row_check_containing_group) {
                if ($row_check_containing_group['field_name'] == $in_group) {
                  if (!conditional_fields_is_triggered($form_state['values'][$row_check_containing_group['control_field_name']], $row_check_containing_group['trigger_values'])) {
                    $set_error = FALSE;
                  }
                  break;
                }
              }
              if ($set_error) {
                form_error($controlled_field, t('!name field is required.', array(
                  '!name' => $controlled_field['#title'],
                )));
              }
            }
          }
        }
      }
      else {

        // Do not submit values of controlled fields which were not triggered (except on preview)
        if (variable_get('c_fields_reset_default_' . $form['type']['#value'], 1) && !in_array('node_form_build_preview', (array) $form_state['submit_handlers'])) {

          // Load default values like in content_field_form() in content.node_form.inc
          $controlled_field_info = $form['#field_info'][$controlled_field_name];
          if (content_callback('widget', 'default value', $controlled_field_info) != CONTENT_CALLBACK_NONE) {
            $callback = content_callback('widget', 'default value', $controlled_field_info) == CONTENT_CALLBACK_CUSTOM ? $controlled_field_info['widget']['module'] . '_default_value' : 'content_default_value';
            if (function_exists($callback)) {
              $items = $callback($form, $form_state, $controlled_field_info, 0);
            }
          }
          $form_state['values'][$controlled_field_name] = $items;
        }
      }
    }
    elseif (strpos($controlled_field_name, 'group_') === 0) {
      foreach (element_children($controlled_field) as $field_in_group) {

        // Check if the controlling field was triggered
        if ($triggered) {

          // Check required
          if (!empty($required_fields) && $required_fields[$field_in_group] && !$controlled_field[$field_in_group]['#controlled_fields'] && conditional_fields_is_empty($form_state['values'][$field_in_group], $form[$controlled_field_name][$field_in_group], $form['#field_info'][$field_in_group])) {
            form_error($controlled_field[$field_in_group], t('!name field is required.', array(
              '!name' => $controlled_field[$field_in_group]['#title'],
            )));
          }
        }
        else {

          // Do not submit values of controlled fields which were not triggered (except on preview)
          if (variable_get('c_fields_reset_default_' . $form['type']['#value'], 1) && !isset($form_state['node_preview'])) {

            // Load default values like in content_field_form() in content.node_form.inc
            $field_in_group_info = $form['#field_info'][$field_in_group];
            if (content_callback('widget', 'default value', $field_in_group_info) != CONTENT_CALLBACK_NONE) {
              $callback = content_callback('widget', 'default value', $field_in_group_info) == CONTENT_CALLBACK_CUSTOM ? $field['widget']['module'] . '_default_value' : 'content_default_value';
              if (function_exists($callback)) {
                $items = $callback($form, $form_state, $field_in_group_info, 0);
              }
            }
            $form_state['values'][$field_in_group] = $items;
          }
        }
      }
    }
  }
}