You are here

function cck_select_other_widget_validate in CCK Select Other 7

Same name and namespace in other branches
  1. 7.2 cck_select_other.module \cck_select_other_widget_validate()

Validate empty text input for other selection.

1 string reference to 'cck_select_other_widget_validate'
cck_select_other_field_widget_form in ./cck_select_other.module
Implementation of hook_field_widget_form().

File

./cck_select_other.module, line 212
Implements a select list widget that lets a user provide an alternate option.

Code

function cck_select_other_widget_validate($element, &$form_state) {

  // Reverse element parents because of element containers, notably profile2.
  $reversed = array_reverse($element['#parents']);
  $element_name = array_shift($reversed);
  $delta = array_shift($reversed);
  $langcode = array_shift($reversed);
  $field_name = array_shift($reversed);
  if (isset($form_state['field'][$field_name])) {

    // Retrieve stored field & instance info and form state values.
    $field = $form_state['field'][$field_name];
    $values =& $form_state['values'];
  }
  elseif (!empty($reversed) && isset($form_state['field']['#parents'])) {

    // Profile 2 exception.
    $container = array_shift($reversed);
    $field = $form_state['field']['#parents'][$container]['#fields'][$field_name];
    $values =& $form_state['values'][$container];
  }
  else {

    // Catastrophic error..?
    form_set_error($element['#name'], t('An error occurred trying to validate this field.'));
    watchdog('cck_select_other', 'Could not find field info in form state array for select other field, %name.', array(
      '%name' => $field_name,
    ), WATCHDOG_ERROR);
  }
  if ($field[$langcode]['instance']['required'] && $values[$field_name][$langcode][$delta]['select_other_list'] == 'other' && empty($values[$field_name][$langcode][$delta]['select_other_text_input'])) {

    // Empty other field.
    form_set_error($element['#name'], t('A non-empty value is required for this option.'));
  }
  if (!$field[$langcode]['instance']['required'] && $values[$field_name][$langcode][$delta]['select_other_list'] == '_none') {

    // Non-required field value.
    form_set_value($element, array(
      NULL,
    ), $form_state);
  }
}