Browse Source

Finished js validation for email and regular expression validators.

tags/2.0.0-beta
Qiang Xue 12 years ago
parent
commit
4b30fe8b5a
  1. 20
      framework/assets/yii.validation.js
  2. 2
      framework/validators/CompareValidator.php
  3. 28
      framework/validators/EmailValidator.php
  4. 25
      framework/validators/RegularExpressionValidator.php
  5. 9
      framework/validators/RequiredValidator.php

20
framework/assets/yii.validation.js

@ -79,6 +79,26 @@ yii.validation = (function ($) {
|| options.strict && (value === options.trueValue || value === options.falseValue); || options.strict && (value === options.trueValue || value === options.falseValue);
valid || messages.push(options.message); 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); })(jQuery);

2
framework/validators/CompareValidator.php

@ -197,7 +197,7 @@ class CompareValidator extends Validator
$options['skipOnEmpty'] = 1; $options['skipOnEmpty'] = 1;
} }
$options['message'] = Html::encode(strtr($options['message'], array( $options['message'] = Html::encode(strtr($this->message, array(
'{attribute}' => $object->getAttributeLabel($attribute), '{attribute}' => $object->getAttributeLabel($attribute),
'{value}' => $object->$attribute, '{value}' => $object->$attribute,
'{compareValue}' => $compareValue, '{compareValue}' => $compareValue,

28
framework/validators/EmailValidator.php

@ -8,6 +8,9 @@
namespace yii\validators; namespace yii\validators;
use Yii; 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. * EmailValidator validates that the attribute value is a valid email address.
@ -100,20 +103,19 @@ class EmailValidator extends Validator
*/ */
public function clientValidateAttribute($object, $attribute) public function clientValidateAttribute($object, $attribute)
{ {
$message = strtr($this->message, array( $options = array(
'{attribute}' => $object->getAttributeLabel($attribute), 'pattern' => new JsExpression($this->pattern),
'{value}' => $object->$attribute, 'fullPattern' => new JsExpression($this->fullPattern),
)); 'allowName' => $this->allowName,
'message' => Html::encode(strtr($this->message, array(
$condition = "!value.match( {$this->pattern})"; '{attribute}' => $object->getAttributeLabel($attribute),
if ($this->allowName) { '{value}' => $object->$attribute,
$condition .= " && !value.match( {$this->fullPattern})"; ))),
);
if ($this->skipOnEmpty) {
$options['skipOnEmpty'] = 1;
} }
return " return 'yii.validation.email(value, messages, ' . Json::encode($options) . ');';
if(" . ($this->skipOnEmpty ? "$.trim(value)!='' && " : '') . $condition . ") {
messages.push(" . json_encode($message) . ");
}
";
} }
} }

25
framework/validators/RegularExpressionValidator.php

@ -9,6 +9,9 @@ namespace yii\validators;
use Yii; use Yii;
use yii\base\InvalidConfigException; 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]]. * RegularExpressionValidator validates that the attribute value matches the specified [[pattern]].
@ -81,11 +84,6 @@ class RegularExpressionValidator extends Validator
*/ */
public function clientValidateAttribute($object, $attribute) public function clientValidateAttribute($object, $attribute)
{ {
$message = strtr($this->message, array(
'{attribute}' => $object->getAttributeLabel($attribute),
'{value}' => $object->$attribute,
));
$pattern = $this->pattern; $pattern = $this->pattern;
$pattern = preg_replace('/\\\\x\{?([0-9a-fA-F]+)\}?/', '\u$1', $pattern); $pattern = preg_replace('/\\\\x\{?([0-9a-fA-F]+)\}?/', '\u$1', $pattern);
$deliminator = substr($pattern, 0, 1); $deliminator = substr($pattern, 0, 1);
@ -100,10 +98,17 @@ class RegularExpressionValidator extends Validator
$pattern .= preg_replace('/[^igm]/', '', $flag); $pattern .= preg_replace('/[^igm]/', '', $flag);
} }
return " $options = array(
if (" . ($this->skipOnEmpty ? "$.trim(value)!='' && " : '') . ($this->not ? '' : '!') . "value.match($pattern)) { 'pattern' => new JsExpression($pattern),
messages.push(" . json_encode($message) . "); '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) . ');';
} }
} }

9
framework/validators/RequiredValidator.php

@ -40,6 +40,15 @@ class RequiredValidator extends Validator
* to check if the attribute value is empty. * to check if the attribute value is empty.
*/ */
public $strict = false; 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. * Initializes the validator.

Loading…
Cancel
Save