You are here

function conditional_fields_node_editing_form_validate in Conditional Fields 6

Same name and namespace in other branches
  1. 5 conditional_fields.module \conditional_fields_node_editing_form_validate()

Validation for node editing form.

1 string reference to 'conditional_fields_node_editing_form_validate'
conditional_fields_node_editing_form in ./conditional_fields.module
Alteration of the node editing form

File

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

Code

function conditional_fields_node_editing_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']);
  }
  foreach ($form['#conditional_fields']['data'] as $row) {

    // Check if the controlling field was triggered
    $triggered = conditional_fields_is_triggered($form_state['values'][$row['control_field_name']], $row['trigger_values']);
    $required_fields = $form['#conditional_fields']['required_fields'];

    // Controlled field
    if (strpos($row['field_name'], 'field_') === 0) {
      if ($triggered) {

        // Check required
        if (!empty($required_fields) && $required_fields[$row['field_name']]) {

          // Check if the controlled field is empty
          if (conditional_fields_check_empty($form_state['values'][$row['field_name']])) {

            // Check whether the controlled field is in a group or not and set error accordingly
            if (!$required_fields[$row['field_name']]['in_group']) {
              form_error($form[$row['field_name']], t('!name field is required.', array(
                '!name' => $form[$row['field_name']]['#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'] == $required_fields[$row['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($form[$required_fields[$row['field_name']]['in_group']][$row['field_name']], t('!name field is required.', array(
                  '!name' => $form[$required_fields[$row['field_name']]['in_group']][$row['field_name']]['#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']) && is_array($form['#field_info'][$row['field_name']]['widget']['default_value'])) {
          foreach ($form['#field_info'][$row['field_name']]['widget']['default_value'] as $delta => $value) {
            $form_state['values'][$row['field_name']][$delta]['value'] = $value['value'];
          }
        }
      }
    }
    elseif (strpos($row['field_name'], 'group_') === 0) {
      foreach (element_children($form[$row['field_name']]) as $field_in_group) {

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

          // Check required
          if (!empty($required_fields) && $required_fields[$field_in_group] && !$form[$row['field_name']][$field_in_group]['#controlled_field'] && conditional_fields_check_empty($form_state['values'][$field_in_group])) {
            form_error($form[$row['field_name']][$field_in_group], t('!name field is required.', array(
              '!name' => $form[$row['field_name']][$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) && !in_array('node_form_build_preview', $form_state['submit_handlers']) && is_array($form['#field_info'][$field_in_group]['widget']['default_value'])) {
            foreach ($form['#field_info'][$field_in_group]['widget']['default_value'] as $delta => $value) {
              $form_state['values'][$field_in_group][$delta]['value'] = $value['value'];
            }
          }
        }
      }
    }
  }
}