You are here

function _hierarchical_select_validate in Hierarchical Select 5.3

Same name and namespace in other branches
  1. 5.2 hierarchical_select.module \_hierarchical_select_validate()
  2. 6.3 hierarchical_select.module \_hierarchical_select_validate()
  3. 7.3 hierarchical_select.module \_hierarchical_select_validate()

Hierarchical select form element #validate callback.

File

./hierarchical_select.module, line 700
This module defines the "hierarchical_select" form element, which is a greatly enhanced way for letting the user select items in a hierarchy.

Code

function _hierarchical_select_validate(&$element) {

  // If the dropbox is enabled and a dropbox limit is configured, check if
  // this limit is not exceeded.
  $config = _hierarchical_select_inherit_default_config($element['#config']);
  if ($config['dropbox']['status']) {
    if ($config['dropbox']['limit'] > 0) {

      // Zero as dropbox limit means no limit.
      // TRICKY: #validate is not called upon the initial rendering (i.e. it
      // is assumed that the default value is valid). However, Hierarchical
      // Select's config can influence the validity (i.e. how many selections
      // may be added to the dropbox). This means it's possible the user has
      // actually selected too many items without being notified of this.
      $lineage_count = count($element['#value']['dropbox']['hidden']['lineages_selections']);
      if ($lineage_count > $config['dropbox']['limit']) {

        // TRICKY: this should propagate the error down to the children, but
        // this doesn't seem to happen, since for example the selects of the
        // hierarchical select don't get the error class set. Further
        // investigation needed.
        form_error($element, t("You've selected %lineage-count items, but you're only allowed to select %dropbox-limit items.", array(
          '%lineage-count' => $lineage_count,
          '%dropbox-limit' => $config['dropbox']['limit'],
        )));
        _hierarchical_select_form_set_error_class($element);
      }
    }
  }

  // Set the proper return value. I.e. instead of returning all the values
  // that are used for making the hierarchical_select form element type work,
  // we pass a flat array of item ids. e.g. for the taxonomy module, this will
  // be an array of term ids. If a single item is selected, this will not be
  // an array.
  // If the form item is disabled, set the default value as the return value,
  // because otherwise nothing would be returned (disabled form items are not
  // submitted, as described in the HTML standard).
  if ($element['#disabled']) {
    $element['#return_value'] = $element['#default_value'];
  }

  // If the array is empty, set 0 as the value, which the Forms API
  // detects as an empty form value.
  $value = empty($element['#return_value']) ? 0 : $element['#return_value'];
  $element['#value'] = $value;
  form_set_value($element, $value);

  // We have to check again for errors. This line is taken litterally from
  // form.inc, so it works in an identical way.
  if ($element['#required'] && empty($element['#value']) && $element['#value'] !== '0') {
    form_error($element, t('!name field is required.', array(
      '!name' => $element['#title'],
    )));
    _hierarchical_select_form_set_error_class($element);
  }
}