You are here

function cck_select_other_widget_validate in CCK Select Other 7.2

Same name and namespace in other branches
  1. 7 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 204
Implements a select list widget that lets a user provide an alternate option.

Code

function cck_select_other_widget_validate($element, &$form_state) {
  $values = drupal_array_get_nested_value($form_state['values'], $element['#array_parents']);
  if (empty($values)) {

    // Field UI does not behave in the same way as normal form operations, and
    // values should be extracted from $element['#parents'] instead.
    $values = drupal_array_get_nested_value($form_state['values'], $element['#parents']);
  }
  if (!$element['select_other_list']['#required'] && $values['select_other_list'] == '_none') {

    // Empty select list option.
    form_set_value($element, array(
      'value' => NULL,
    ), $form_state);
  }
  elseif ($element['select_other_list']['#required'] && $values['select_other_list'] == '') {

    // Empty select list option for required field.
    form_set_value($element, array(
      'value' => '',
    ), $form_state);
    form_error($element, t('You must select an option.'));
  }
  elseif ($element['select_other_list']['#required'] && $values['select_other_list'] == 'other' && !$values['select_other_text_input']) {

    // Empty text input for required field.
    form_set_value($element, array(
      'value' => NULL,
    ), $form_state);
    form_error($element['select_other_text_input'], t('You must provide a value for this option.'));
  }
  elseif ($values['select_other_list'] == 'other' && $values['select_other_text_input']) {

    // Non-empty text input value.
    form_set_value($element, array(
      'value' => $values['select_other_text_input'],
    ), $form_state);
  }
  elseif ($values['select_other_list'] == 'other' && !$values['select_other_text_input']) {

    // Empty text for non-required field.
    form_set_value($element, array(
      'value' => NULL,
    ), $form_state);
  }
  else {

    // Non-empty select list value.
    form_set_value($element, array(
      'value' => $values['select_other_list'],
    ), $form_state);
  }
  $field = field_info_field($element['#field_name']);

  // Validate integer and float values for other options.
  if ($field['type'] == 'list_integer' && $values['select_other_list'] == 'other') {
    if (!preg_match('/^-?\\d+$/', $values['select_other_text_input'])) {
      form_error($element['select_other_text_input'], t('Only integers are allowed.'));
    }
  }
  elseif ($field['type'] == 'list_float' && $values['select_other_list'] == 'other') {
    if (!is_numeric($values['select_other_text_input'])) {
      form_error($element['select_other_text_input'], t('Only valid numbers are allowed.'));
    }
  }
}