From 08c3f90d4853268fc4be2742453abd14e3470f6b Mon Sep 17 00:00:00 2001 From: Qiang Xue Date: Thu, 2 May 2013 11:29:23 -0400 Subject: [PATCH] Finished js validation for number validator. --- framework/assets/yii.validation.js | 20 +++++++- framework/validators/NumberValidator.php | 53 +++++++++------------- .../validators/RegularExpressionValidator.php | 1 + framework/validators/StringValidator.php | 43 ++++++++---------- 4 files changed, 61 insertions(+), 56 deletions(-) diff --git a/framework/assets/yii.validation.js b/framework/assets/yii.validation.js index 858c028..495a72c 100644 --- a/framework/assets/yii.validation.js +++ b/framework/assets/yii.validation.js @@ -86,7 +86,7 @@ yii.validation = (function ($) { return; } - if (!value.match(options.pattern)) { + if (!options.not && !value.match(options.pattern) || options.not && value.match(options.pattern)) { messages.push(options.message) } }, @@ -134,6 +134,24 @@ yii.validation = (function ($) { if (options.is !== undefined && value.length != options.is) { messages.push(options.is); } + }, + + number: function (value, messages, options) { + if (options.skipOnEmpty && isEmpty(value)) { + return; + } + + if (typeof value === 'string' && !value.match(options.pattern)) { + messages.push(options.message); + return; + } + + if (options.min !== undefined && value < options.min) { + messages.push(options.tooSmall); + } + if (options.max !== undefined && value > options.max) { + messages.push(options.tooBig); + } } }; })(jQuery); diff --git a/framework/validators/NumberValidator.php b/framework/validators/NumberValidator.php index 915419e..d0a4002 100644 --- a/framework/validators/NumberValidator.php +++ b/framework/validators/NumberValidator.php @@ -8,6 +8,9 @@ namespace yii\validators; use Yii; +use yii\helpers\Html; +use yii\helpers\JsExpression; +use yii\helpers\Json; /** * NumberValidator validates that the attribute value is a number. @@ -116,48 +119,36 @@ class NumberValidator extends Validator public function clientValidateAttribute($object, $attribute) { $label = $object->getAttributeLabel($attribute); - $message = strtr($this->message, array( - '{attribute}' => $label, - )); + $value = $object->$attribute; + + $options = array( + 'pattern' => new JsExpression($this->integerOnly ? $this->integerPattern : $this->numberPattern), + 'message' => Html::encode(strtr($this->message, array( + '{attribute}' => $label, + '{value}' => $value, + ))), + ); - $pattern = $this->integerOnly ? $this->integerPattern : $this->numberPattern; - $js = " -if(!value.match($pattern)) { - messages.push(" . json_encode($message) . "); -} -"; if ($this->min !== null) { - $tooSmall = strtr($this->tooSmall, array( + $options['min'] = $this->min; + $options['tooSmall'] = Html::encode(strtr($this->tooSmall, array( '{attribute}' => $label, + '{value}' => $value, '{min}' => $this->min, - )); - - $js .= " -if(value<{$this->min}) { - messages.push(" . json_encode($tooSmall) . "); -} -"; + ))); } if ($this->max !== null) { - $tooBig = strtr($this->tooBig, array( + $options['max'] = $this->max; + $options['tooBig'] = Html::encode(strtr($this->tooBig, array( '{attribute}' => $label, + '{value}' => $value, '{max}' => $this->max, - )); - $js .= " -if(value>{$this->max}) { - messages.push(" . json_encode($tooBig) . "); -} -"; + ))); } - if ($this->skipOnEmpty) { - $js = " -if(jQuery.trim(value)!='') { - $js -} -"; + $options['skipOnEmpty'] = 1; } - return $js; + return 'yii.validation.number(value, messages, ' . Json::encode($options) . ');'; } } \ No newline at end of file diff --git a/framework/validators/RegularExpressionValidator.php b/framework/validators/RegularExpressionValidator.php index f3f34f6..79a1a3c 100644 --- a/framework/validators/RegularExpressionValidator.php +++ b/framework/validators/RegularExpressionValidator.php @@ -100,6 +100,7 @@ class RegularExpressionValidator extends Validator $options = array( 'pattern' => new JsExpression($pattern), + 'not' => $this->not, 'message' => Html::encode(strtr($this->message, array( '{attribute}' => $object->getAttributeLabel($attribute), '{value}' => $object->$attribute, diff --git a/framework/validators/StringValidator.php b/framework/validators/StringValidator.php index 83a5eba..5d0fa1a 100644 --- a/framework/validators/StringValidator.php +++ b/framework/validators/StringValidator.php @@ -133,41 +133,36 @@ class StringValidator extends Validator $label = $object->getAttributeLabel($attribute); $value = $object->$attribute; - $message = strtr($this->message, array( - '{attribute}' => $label, - '{value}' => $value, - )); - $notEqual = strtr($this->notEqual, array( - '{attribute}' => $label, - '{value}' => $value, - '{length}' => $this->is, - )); - $tooShort = strtr($this->tooShort, array( - '{attribute}' => $label, - '{value}' => $value, - '{min}' => $this->min, - )); - $tooLong = strtr($this->tooLong, array( - '{attribute}' => $label, - '{value}' => $value, - '{max}' => $this->max, - )); - $options = array( - 'message' => Html::encode($message), - 'notEqual' => Html::encode($notEqual), - 'tooShort' => Html::encode($tooShort), - 'tooLong' => Html::encode($tooLong), + 'message' => Html::encode(strtr($this->message, array( + '{attribute}' => $label, + '{value}' => $value, + ))), ); if ($this->min !== null) { $options['min'] = $this->min; + $options['tooShort'] = Html::encode(strtr($this->tooShort, array( + '{attribute}' => $label, + '{value}' => $value, + '{min}' => $this->min, + ))); } if ($this->max !== null) { $options['max'] = $this->max; + $options['tooLong'] = Html::encode(strtr($this->tooLong, array( + '{attribute}' => $label, + '{value}' => $value, + '{max}' => $this->max, + ))); } if ($this->is !== null) { $options['is'] = $this->is; + $options['notEqual'] = Html::encode(strtr($this->notEqual, array( + '{attribute}' => $label, + '{value}' => $value, + '{length}' => $this->is, + ))); } if ($this->skipOnEmpty) { $options['skipOnEmpty'] = 1;