You are here

function content_taxonomy_autocomplete_validate in Content Taxonomy 6.2

Same name and namespace in other branches
  1. 6 content_taxonomy_autocomplete.module \content_taxonomy_autocomplete_validate()

Validation function for the content_taxonomy_autocomplete element

parses input, handles new terms (depending on settings) and sets the values as needed for storing the data

1 string reference to 'content_taxonomy_autocomplete_validate'
content_taxonomy_autocomplete_process in ./content_taxonomy_autocomplete.module
Process an individual element.

File

./content_taxonomy_autocomplete.module, line 207
Defines a widget type for content_taxonomy with autocomplete

Code

function content_taxonomy_autocomplete_validate($element, &$form_state) {
  $field_name = $element['#field_name'];
  $field = content_fields($field_name, $element['#type_name']);
  $field_key = $element['#columns'][0];

  //if the element parents array contains the field key, we have to remove it

  //because otherwise form_set_value won't work. (still the question why is it in)
  if ($element['#parents'][count($element['#parents']) - 1] == $field_key) {
    array_pop($element['#parents']);
    array_pop($element['#array_parents']);
  }
  $value = $element['#value'];
  $extracted_ids = content_taxonomy_autocomplete_tags_get_tids($value, $field['vid'], content_taxonomy_field_get_parent($field), $field['widget']['extra_parent']);
  if (!$field['multiple'] && count(content_taxonomy_autocomplete_split_tags($value, $field['vid'])) > 1) {
    form_set_error($field['field_name'] . '][value', t('You can provide only one value'));
    return;
  }
  else {
    if ($field['multiple'] >= 2 && count(content_taxonomy_autocomplete_split_tags($value, $field['vid'])) > $field['multiple']) {
      form_set_error($field['field_name'] . '][value', t('%name: this field cannot hold more than @count values.', array(
        '%name' => t($field['widget']['label']),
        '@count' => $field['multiple'],
      )));
    }
  }
  if ($field['widget']['new_terms'] == 'deny') {
    if (is_array($extracted_ids['non_existing_terms'])) {
      form_set_error($field['field_name'] . '][value', t('New tags are not allowed'));
      return;
    }
  }
  $values = content_taxonomy_autocomplete_form2data($extracted_ids, $field, $element);
  form_set_value($element, $values, $form_state);
}