ActiveRecord\Validations::validates_length_of PHP Method

validates_length_of() public method

class Person extends ActiveRecord\Model { static $validates_length_of = array( array('name', 'within' => array(1,50)) ); } Available options:
  • is: attribute should be exactly n characters long
  • in/within: attribute should be within an range array(min,max)
  • maximum/minimum: attribute should not be above/below respectively
  • message: custome error message
  • allow_blank: allow blank strings
  • allow_null: allow null strings. (Even if this is set to false, a null string is always shorter than a maximum value.)
public validates_length_of ( array $attrs )
$attrs array Validation definition
    public function validates_length_of($attrs)
    {
        $configuration = array_merge(self::$DEFAULT_VALIDATION_OPTIONS, array('too_long' => Errors::$DEFAULT_ERROR_MESSAGES['too_long'], 'too_short' => Errors::$DEFAULT_ERROR_MESSAGES['too_short'], 'wrong_length' => Errors::$DEFAULT_ERROR_MESSAGES['wrong_length']));
        foreach ($attrs as $attr) {
            $options = array_merge($configuration, $attr);
            $range_options = array_intersect(array_keys(self::$ALL_RANGE_OPTIONS), array_keys($attr));
            sort($range_options);
            switch (sizeof($range_options)) {
                case 0:
                    throw new ValidationsArgumentError('Range unspecified.  Specify the [within], [maximum], or [is] option.');
                case 1:
                    break;
                default:
                    throw new ValidationsArgumentError('Too many range options specified.  Choose only one.');
            }
            $attribute = $options[0];
            $var = $this->model->{$attribute};
            if ($this->is_null_with_option($var, $options) || $this->is_blank_with_option($var, $options)) {
                continue;
            }
            if ($range_options[0] == 'within' || $range_options[0] == 'in') {
                $range = $options[$range_options[0]];
                if (!Utils::is_a('range', $range)) {
                    throw new ValidationsArgumentError("{$range_options['0']} must be an array composing a range of numbers with key [0] being less than key [1]");
                }
                $range_options = array('minimum', 'maximum');
                $attr['minimum'] = $range[0];
                $attr['maximum'] = $range[1];
            }
            foreach ($range_options as $range_option) {
                $option = $attr[$range_option];
                if ((int) $option <= 0) {
                    throw new ValidationsArgumentError("{$range_option} value cannot use a signed integer.");
                }
                if (is_float($option)) {
                    throw new ValidationsArgumentError("{$range_option} value cannot use a float for length.");
                }
                if (!($range_option == 'maximum' && is_null($this->model->{$attribute}))) {
                    $messageOptions = array('is' => 'wrong_length', 'minimum' => 'too_short', 'maximum' => 'too_long');
                    if (isset($options['message'])) {
                        $message = $options['message'];
                    } else {
                        $message = $options[$messageOptions[$range_option]];
                    }
                    $message = str_replace('%d', $option, $message);
                    $attribute_value = $this->model->{$attribute};
                    $len = strlen($attribute_value);
                    $value = (int) $attr[$range_option];
                    if ('maximum' == $range_option && $len > $value) {
                        $this->record->add($attribute, $message);
                    }
                    if ('minimum' == $range_option && $len < $value) {
                        $this->record->add($attribute, $message);
                    }
                    if ('is' == $range_option && $len !== $value) {
                        $this->record->add($attribute, $message);
                    }
                }
            }
        }
    }