You are here

function og_vocab_sub_widget_cardinality_validate in OG Vocabulary 7

Element validate; Check cardinality of sub-widget.

1 string reference to 'og_vocab_sub_widget_cardinality_validate'
OgVocab::getFormElement in includes/og_vocab.og_vocab.inc
Return form element.

File

includes/og_vocab.og_vocab.inc, line 266
A class used for messages.

Code

function og_vocab_sub_widget_cardinality_validate($element, &$form_state) {
  $og_vocab = $element['#og_vocab'];
  $cardinality = $og_vocab->settings['cardinality'];
  $field_name = $og_vocab->field_name;
  if ($cardinality == FIELD_CARDINALITY_UNLIMITED) {
    return;
  }
  if (empty($form_state['values'][$field_name][LANGUAGE_NONE][0])) {
    return;
  }
  $vid = $og_vocab->vid;
  $count = 0;
  foreach ($form_state['values'][$field_name][LANGUAGE_NONE][0][$vid] as $value) {
    if (!empty($value['target_id'])) {
      ++$count;
      if ($count > $cardinality) {
        $vocabulary = taxonomy_vocabulary_load($vid);
        $params = array(
          '@name' => $vocabulary->name,
          '@cardinality' => $cardinality,
        );
        form_error($element, t('@name: this field cannot hold more than @cardinality values', $params));
        return;
      }
    }
  }
}