From 4e7a33bcf5b9be7fb0ccde5301a935a9574cb048 Mon Sep 17 00:00:00 2001 From: Qiang Xue Date: Thu, 2 May 2013 11:16:31 -0400 Subject: [PATCH] Finished js validation for string validator. --- framework/assets/yii.validation.js | 21 ++++++++++++++++ framework/validators/StringValidator.php | 42 +++++++++++++------------------- 2 files changed, 38 insertions(+), 25 deletions(-) diff --git a/framework/assets/yii.validation.js b/framework/assets/yii.validation.js index b663470..858c028 100644 --- a/framework/assets/yii.validation.js +++ b/framework/assets/yii.validation.js @@ -114,5 +114,26 @@ yii.validation = (function ($) { messages.push(options.message); } }, + + string: function (value, messages, options) { + if (options.skipOnEmpty && isEmpty(value)) { + return; + } + + if (typeof value !== 'string') { + messages.push(options.message); + return; + } + + if (options.min !== undefined && value.length < options.min) { + messages.push(options.tooShort); + } + if (options.max !== undefined && value.length > options.max) { + messages.push(options.tooLong); + } + if (options.is !== undefined && value.length != options.is) { + messages.push(options.is); + } + } }; })(jQuery); diff --git a/framework/validators/StringValidator.php b/framework/validators/StringValidator.php index 8b8c73b..83a5eba 100644 --- a/framework/validators/StringValidator.php +++ b/framework/validators/StringValidator.php @@ -8,6 +8,7 @@ namespace yii\validators; use Yii; +use yii\helpers\Html; /** * StringValidator validates that the attribute value is of certain length. @@ -132,56 +133,47 @@ 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, )); - $js = ''; + $options = array( + 'message' => Html::encode($message), + 'notEqual' => Html::encode($notEqual), + 'tooShort' => Html::encode($tooShort), + 'tooLong' => Html::encode($tooLong), + ); + if ($this->min !== null) { - $js .= " -if(value.length< {$this->min}) { - messages.push(" . json_encode($tooShort) . "); -} -"; + $options['min'] = $this->min; } if ($this->max !== null) { - $js .= " -if(value.length> {$this->max}) { - messages.push(" . json_encode($tooLong) . "); -} -"; + $options['max'] = $this->max; } if ($this->is !== null) { - $js .= " -if(value.length!= {$this->is}) { - messages.push(" . json_encode($notEqual) . "); -} -"; + $options['is'] = $this->is; } - if ($this->skipOnEmpty) { - $js = " -if($.trim(value)!='') { - $js -} -"; + $options['skipOnEmpty'] = 1; } - return $js; + return 'yii.validation.string(value, messages, ' . json_encode($options) . ');'; } }