You are here

function field_hidden_field_validate in Field Hidden 7

Validation of field items upon form submission.

Text field error codes:

  • 'text_utf8': Value not UTF-8
  • 'text_plain': Value not plaintext

Numeric field error codes:

  • 'number_chars': The value contains illegal character(s).
  • 'number_min': The value is less than the allowed minimum value.
  • 'number_max': The value is greater than the allowed maximum value.

All fields error codes:

  • 'text_length': Value too long

Integer and decimal types are being checked against absolute minimum/maximum as well as instance settings min/max.

Implements hook_field_validate().

Parameters

string $entity_type:

string $entity:

array $field:

array $instance:

string $langcode:

array $items:

array &$errors:

Return value

void

File

./field_hidden.module, line 366
Drupal Field Hidden module

Code

function field_hidden_field_validate($entity_type, $entity, $field, $instance, $langcode, $items, &$errors) {

  //$fld_set = $field['settings'];

  //$inst_set = $instance['settings']; // unhealthy to put here, because text and text_long have no instance settings
  foreach ($items as $delta => $item) {
    if ($item['value'] != '') {
      $value = $item['value'];
      switch ($field['type']) {
        case 'field_hidden_text':
        case 'field_hidden_text_long':
          $fld_set = $field['settings'];
          if (!drupal_validate_utf8($value)) {
            $errors[$field['field_name']][$langcode][$delta][] = array(
              'error' => 'text_utf8',
              'message' => t('%name: the value is not valid UTF-8.', array(
                '%name' => $instance['label'],
              )),
            );
          }
          elseif (is_numeric($fld_set['max_length']) && drupal_strlen($value) > $fld_set['max_length']) {
            $errors[$field['field_name']][$langcode][$delta][] = array(
              'error' => 'text_length',
              'message' => t('%name: the length (!n) cannot be more than %length.', array(
                '%name' => $instance['label'],
                '!n' => drupal_strlen($value),
                '%length' => $fld_set['max_length'],
              )),
            );
          }
          elseif (!_field_hidden_check_plaintext($value)) {
            $errors[$field['field_name']][$langcode][$delta][] = array(
              'error' => 'text_plain',
              'message' => t('%name: the value is not plain text, appears to contain html markup.', array(
                '%name' => $instance['label'],
              )),
            );
          }
          break;
        case 'field_hidden_integer':
          $inst_set = $instance['settings'];
          if (!_field_hidden_check_numeric($value)) {
            $errors[$field['field_name']][$langcode][$delta][] = array(
              'error' => 'number_chars',
              'message' => t('%name: the value is not an integer.', array(
                '%name' => $instance['label'],
              )),
            );
          }
          else {
            $float = (double) $value;

            // float can hold larger and smaller value than integer
            $int = (int) $value;
            if ($value != '-2147483648' && $float < -2147483648) {

              // absolute minimum
              $errors[$field['field_name']][$langcode][$delta][] = array(
                'error' => 'number_min',
                'message' => t('%name: the value may be no less than -2147483648 (32-bit integer).', array(
                  '%name' => $instance['label'],
                )),
              );
            }
            elseif (is_numeric($inst_set['min']) && $int < $inst_set['min']) {
              $errors[$field['field_name']][$langcode][$delta][] = array(
                'error' => 'number_min',
                'message' => t('%name: the value may be no less than %min.', array(
                  '%name' => $instance['label'],
                  '%min' => $inst_set['min'],
                )),
              );
            }
            elseif ($value != '2147483647' && $float > 2147483647) {

              // absolute maximum
              $errors[$field['field_name']][$langcode][$delta][] = array(
                'error' => 'number_max',
                'message' => t('%name: the value may be no greater than 2147483647 (32-bit integer).', array(
                  '%name' => $instance['label'],
                )),
              );
            }
            elseif (is_numeric($inst_set['max']) && $int > $inst_set['max']) {
              $errors[$field['field_name']][$langcode][$delta][] = array(
                'error' => 'number_max',
                'message' => t('%name: the value may be no greater than %max.', array(
                  '%name' => $instance['label'],
                  '%max' => $inst_set['max'],
                )),
              );
            }
          }
          break;
        case 'field_hidden_decimal':
          $inst_set = $instance['settings'];
          if (!_field_hidden_check_numeric($value, TRUE)) {
            $errors[$field['field_name']][$langcode][$delta][] = array(
              'error' => 'number_chars',
              'message' => t('%name: the value is not a decimal number.', array(
                '%name' => $instance['label'],
              )),
            );
          }
          else {
            $float = (double) $value;
            $maxByPrecision = (pow(10, $field['settings']['precision']) - 1) / pow(10, $field['settings']['scale']);
            if ($value != '-' . $maxByPrecision && $float < -$maxByPrecision) {

              // absolute minimum
              $errors[$field['field_name']][$langcode][$delta][] = array(
                'error' => 'number_min',
                'message' => t('%name: the value may be no less than (precision/scale) -%min.', array(
                  '%name' => $instance['label'],
                  '%min' => $maxByPrecision,
                )),
              );
            }
            elseif (is_numeric($inst_set['min']) && $float < $inst_set['min']) {
              $errors[$field['field_name']][$langcode][$delta][] = array(
                'error' => 'number_min',
                'message' => t('%name: the value may be no less than %min.', array(
                  '%name' => $instance['label'],
                  '%min' => $inst_set['min'],
                )),
              );
            }
            elseif ($value != '' . $maxByPrecision && $float > $maxByPrecision) {

              // absolute maximum
              $errors[$field['field_name']][$langcode][$delta][] = array(
                'error' => 'number_max',
                'message' => t('%name: the value may be no greater than (precision/scale) %max.', array(
                  '%name' => $instance['label'],
                  '%max' => $maxByPrecision,
                )),
              );
            }
            elseif (is_numeric($inst_set['max']) && $float > $inst_set['max']) {
              $errors[$field['field_name']][$langcode][$delta][] = array(
                'error' => 'number_max',
                'message' => t('%name: the value may be no greater than %max.', array(
                  '%name' => $instance['label'],
                  '%max' => $inst_set['max'],
                )),
              );
            }
          }
          break;
        case 'field_hidden_float':

          //  Uncertain absolute min/max (for PHP as well as database), thus no check for that.
          //  Using the float types isn't really recommendable, decimal is usually a much better choice.
          $inst_set = $instance['settings'];
          if (!_field_hidden_check_numeric($value, TRUE)) {
            $errors[$field['field_name']][$langcode][$delta][] = array(
              'error' => 'number_chars',
              'message' => t('%name: the value is not a floating point number.', array(
                '%name' => $instance['label'],
              )),
            );
          }
          else {
            $float = (double) $value;
            if (is_numeric($inst_set['min']) && $float < $inst_set['min']) {
              $errors[$field['field_name']][$langcode][$delta][] = array(
                'error' => 'number_min',
                'message' => t('%name: the value may be no less than %min.', array(
                  '%name' => $instance['label'],
                  '%min' => $inst_set['min'],
                )),
              );
            }
            elseif (is_numeric($inst_set['max']) && $float > $inst_set['max']) {
              $errors[$field['field_name']][$langcode][$delta][] = array(
                'error' => 'number_max',
                'message' => t('%name: the value may be no greater than %max.', array(
                  '%name' => $instance['label'],
                  '%max' => $inst_set['max'],
                )),
              );
            }
          }
          break;
      }
    }
  }
}