You are here

function select_or_other_element_validate in Select (or other) 7.2

Same name and namespace in other branches
  1. 6.2 select_or_other.module \select_or_other_element_validate()
  2. 6 select_or_other.module \select_or_other_element_validate()
  3. 7.3 select_or_other.module \select_or_other_element_validate()

Element validate callback for a Select (or other) element.

1 string reference to 'select_or_other_element_validate'
select_or_other_element_info in ./select_or_other.module
Implements hook_element_info().

File

./select_or_other.module, line 212
The Select (or other) module.

Code

function select_or_other_element_validate($element, &$form_state) {
  $other_selected = FALSE;
  if (is_array($element['select']['#value']) && isset($element['select']['#value']['select_or_other'])) {

    // This is a multiselect. assoc arrays
    $other_selected = TRUE;
    $value = $element['select']['#value'];
    unset($value['select_or_other']);
    $value[$element['other']['#value']] = $element['other']['#value'];
  }
  elseif (is_string($element['select']['#value']) && $element['select']['#value'] == 'select_or_other') {

    // This is a single select.
    $other_selected = TRUE;
    $value = $element['other']['#value'];
  }
  else {
    $value = $element['select']['#value'];
  }
  if ($other_selected && $element['other']['#value'] === '') {
    form_error($element['other'], t('!title field is required.', array(
      '!title' => $element['select']['#title'],
    )));
  }
  if (isset($value)) {
    form_set_value($element, $value, $form_state);
    $form_state['clicked_button']['#post'][$element['#name']] = $value;

    // Is this something we should do?
  }
  return $element;
}