00001 <?php
00002
00006 class spunQ_StringLengthValidator extends spunQ_FieldValidator {
00007
00012 protected $min;
00013
00018 protected $max;
00019
00029 public function __construct($min = NULL, $max = NULL, $messageKey = 'InvalidLength') {
00030 $this->min = $min;
00031 $this->max = $max;
00032 parent::__construct($messageKey);
00033 return NULL;
00034 }
00035
00039 public function validate($messagePrefix, $value) {
00040 if ($value === NULL) {
00041 return NULL;
00042 }
00043 if (!is_string($value)) {
00044 throw new spunQ_HtmlException('spunQ.html.form.validation.StringLengthValidator.NotAString', spunQ_Type::getDescription($value));
00045 }
00046 if ($this->min === NULL && $this->max === NULL) {
00047 return NULL;
00048 }
00049 $length = mb_strlen($value);
00050 if ($this->min !== NULL && $length < $this->min) {
00051 return lang()->translate($messagePrefix . $this->messageKey, array($length, $this->min, $this->max));
00052 }
00053 if ($this->max !== NULL && $length > $this->max) {
00054 return lang()->translate($messagePrefix . $this->messageKey, array($length, $this->min, $this->max));
00055 }
00056 return NULL;
00057 }
00058
00062 public function showJsValidationString($typePath, $messagePrefix) {
00063 $min = $this->min === NULL ? 'null' : $this->min;
00064 $max = $this->max === NULL ? 'null' : $this->max;
00065 $message = lang()->translate($messagePrefix . $this->messageKey, array('{3}', $this->min, $this->max));
00066 echo "spunQ.form.registerValidator('$typePath', spunQ.form.validator.StringLengthValidator('$min', '$max', '$message'));\n";
00067 return true;
00068 }
00069
00070 }
00071