You are here

function barcode_element_validate in Barcode 7.2

Same name and namespace in other branches
  1. 6.2 barcode.module \barcode_element_validate()

Validate an individual barcode element.

1 string reference to 'barcode_element_validate'
barcode_field_widget_form in ./barcode.module
Implements hook_field_widget_form().

File

./barcode.module, line 107

Code

function barcode_element_validate($element, &$form_state) {
  $value = trim($element['#value'], " \0\t\v");
  form_set_value($element, $value, $form_state);
  $length = strlen($value);

  // Check length. @TODO we need to set up plugins in a more modular way
  // so they can handle their own validation.
  if ($value != '') {
    switch ($element['#encoding']) {
      case 'EAN-8':
        if ($length != $element['#maxlength'] || !is_numeric($value)) {
          form_error($element, t('EAN-8 barcode must have 8 digits'));
        }
      case 'EAN-13':
        if ($length != $element['#maxlength'] || !is_numeric($value)) {
          form_error($element, t('EAN-13 barcode must have 13 digits'));
        }
      case 'ISBN':
        if ($length != $element['#maxlength']) {
          form_error($element, t('The ISBN barcode must have %count digits.', array(
            '%count' => $element['#maxlength'],
          )));
        }
        break;
      case 'POSTNET':
        if ($length != 5 and $length != 9 and $length != 11) {
          form_error($element, t('Postnet number must have 5, 9 or 11 digits.'));
        }
        break;
      case 'UPC-A':
        if ($length != 12) {
          form_error($element, t('UPC-A code must have 12 digits.'));
        }
        break;
      case 'ISBN':
        if (substr($value, 0, 3) != '978') {
          form_error($element, t('ISBN barcode number must start with 978.'));
        }
        break;
    }
  }
  return $element;
}