You are here

function select_or_other_cck_validate in Select (or other) 6

Same name and namespace in other branches
  1. 6.2 select_or_other.module \select_or_other_cck_validate()

Element validate callback for a Select (or other) CCK widget.

1 string reference to 'select_or_other_cck_validate'
select_or_other_widget in ./select_or_other.module
Implementation of hook_widget().

File

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

Code

function select_or_other_cck_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_state['values']['form_id'] != 'content_field_edit_form') {
    form_error($element['other'], t('%name: @title is required', array(
      '%name' => t($element['select']['#title']),
      '@title' => $element['#other'],
    )));
  }
  if (isset($value)) {
    if (in_array($element['#cck_widget'], array(
      'select_or_other',
      'select_or_other_buttons',
    ))) {

      // Filter out 'none' value (if present, will always be in key 0)
      if (isset($items[0]['value']) && $items[0]['value'] === '') {
        unset($items[0]);
      }
      if ($element['#multiple'] >= 2 && count($value) > $element['#multiple']) {
        form_error($element['select'], t('%name: this field cannot hold more than @count values.', array(
          '%name' => t($element['select']['#title']),
          '@count' => $element['#multiple'],
        )));
      }
      $delta = 0;
      $values = array();
      foreach ((array) $value as $v) {
        $values[$delta++]['value'] = $v;
      }
      $value = $values;
    }
    else {
      if ($element['#cck_widget'] == 'select_or_other_sort') {
        $value = array(
          'value' => $value,
        );
      }
    }
    form_set_value($element, $value, $form_state);
    $form_state['clicked_button']['#post'][$element['#name']] = $value;

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