You are here

function select_or_other_element_validate in Select (or other) 6

Same name and namespace in other branches
  1. 6.2 select_or_other.module \select_or_other_element_validate()
  2. 7.3 select_or_other.module \select_or_other_element_validate()
  3. 7.2 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_elements in ./select_or_other.module
Implementation of hook_elements().

File

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

Code

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

    // 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'];
  }
  else {
    if (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 is required', array(
      '!title' => $element['#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;
}