Commit 4b30fe8b by Qiang Xue

Finished js validation for email and regular expression validators.

parent 41b127b9
......@@ -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);
......@@ -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,
......
......@@ -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) . ');';
}
}
......@@ -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) . ');';
}
}
......@@ -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.
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment