diff --git a/framework/assets/yii.validation.js b/framework/assets/yii.validation.js index 0865237..8ed7f61 100644 --- a/framework/assets/yii.validation.js +++ b/framework/assets/yii.validation.js @@ -79,6 +79,26 @@ yii.validation = (function ($) { || options.strict && (value === options.trueValue || value === options.falseValue); valid || messages.push(options.message); + }, + + email: function (value, messages, options) { + if (options.skipOnEmpty && isEmpty(value)) { + return; + } + + var valid = value.match(options.pattern) && (!options.allowName || value.match(options.fullPattern)); + + valid || messages.push(options.message); + }, + + regularExpression: function (value, messages, options) { + if (options.skipOnEmpty && isEmpty(value)) { + return; + } + + if (!value.match(options.pattern)) { + messages.push(options.message) + } } }; })(jQuery); diff --git a/framework/validators/CompareValidator.php b/framework/validators/CompareValidator.php index 720ab56..68504e5 100644 --- a/framework/validators/CompareValidator.php +++ b/framework/validators/CompareValidator.php @@ -197,7 +197,7 @@ class CompareValidator extends Validator $options['skipOnEmpty'] = 1; } - $options['message'] = Html::encode(strtr($options['message'], array( + $options['message'] = Html::encode(strtr($this->message, array( '{attribute}' => $object->getAttributeLabel($attribute), '{value}' => $object->$attribute, '{compareValue}' => $compareValue, diff --git a/framework/validators/EmailValidator.php b/framework/validators/EmailValidator.php index e498975..ad74dd6 100644 --- a/framework/validators/EmailValidator.php +++ b/framework/validators/EmailValidator.php @@ -8,6 +8,9 @@ namespace yii\validators; use Yii; +use yii\helpers\Html; +use yii\helpers\JsExpression; +use yii\helpers\Json; /** * EmailValidator validates that the attribute value is a valid email address. @@ -100,20 +103,19 @@ class EmailValidator extends Validator */ public function clientValidateAttribute($object, $attribute) { - $message = strtr($this->message, array( - '{attribute}' => $object->getAttributeLabel($attribute), - '{value}' => $object->$attribute, - )); - - $condition = "!value.match( {$this->pattern})"; - if ($this->allowName) { - $condition .= " && !value.match( {$this->fullPattern})"; + $options = array( + 'pattern' => new JsExpression($this->pattern), + 'fullPattern' => new JsExpression($this->fullPattern), + 'allowName' => $this->allowName, + 'message' => Html::encode(strtr($this->message, array( + '{attribute}' => $object->getAttributeLabel($attribute), + '{value}' => $object->$attribute, + ))), + ); + if ($this->skipOnEmpty) { + $options['skipOnEmpty'] = 1; } - return " -if(" . ($this->skipOnEmpty ? "$.trim(value)!='' && " : '') . $condition . ") { - messages.push(" . json_encode($message) . "); -} -"; + return 'yii.validation.email(value, messages, ' . Json::encode($options) . ');'; } } diff --git a/framework/validators/RegularExpressionValidator.php b/framework/validators/RegularExpressionValidator.php index 6c69be3..f3f34f6 100644 --- a/framework/validators/RegularExpressionValidator.php +++ b/framework/validators/RegularExpressionValidator.php @@ -9,6 +9,9 @@ namespace yii\validators; use Yii; use yii\base\InvalidConfigException; +use yii\helpers\Html; +use yii\helpers\JsExpression; +use yii\helpers\Json; /** * RegularExpressionValidator validates that the attribute value matches the specified [[pattern]]. @@ -81,11 +84,6 @@ class RegularExpressionValidator extends Validator */ public function clientValidateAttribute($object, $attribute) { - $message = strtr($this->message, array( - '{attribute}' => $object->getAttributeLabel($attribute), - '{value}' => $object->$attribute, - )); - $pattern = $this->pattern; $pattern = preg_replace('/\\\\x\{?([0-9a-fA-F]+)\}?/', '\u$1', $pattern); $deliminator = substr($pattern, 0, 1); @@ -100,10 +98,17 @@ class RegularExpressionValidator extends Validator $pattern .= preg_replace('/[^igm]/', '', $flag); } - return " -if (" . ($this->skipOnEmpty ? "$.trim(value)!='' && " : '') . ($this->not ? '' : '!') . "value.match($pattern)) { - messages.push(" . json_encode($message) . "); -} -"; + $options = array( + 'pattern' => new JsExpression($pattern), + 'message' => Html::encode(strtr($this->message, array( + '{attribute}' => $object->getAttributeLabel($attribute), + '{value}' => $object->$attribute, + ))), + ); + if ($this->skipOnEmpty) { + $options['skipOnEmpty'] = 1; + } + + return 'yii.validation.regularExpression(value, messages, ' . Json::encode($options) . ');'; } } diff --git a/framework/validators/RequiredValidator.php b/framework/validators/RequiredValidator.php index 7202ec3..4c14a8d 100644 --- a/framework/validators/RequiredValidator.php +++ b/framework/validators/RequiredValidator.php @@ -40,6 +40,15 @@ class RequiredValidator extends Validator * to check if the attribute value is empty. */ public $strict = false; + /** + * @var string the user-defined error message. It may contain the following placeholders which + * will be replaced accordingly by the validator: + * + * - `{attribute}`: the label of the attribute being validated + * - `{value}`: the value of the attribute being validated + * - `{requiredValue}`: the value of [[requiredValue]] + */ + public $message; /** * Initializes the validator.