Commit 08c3f90d by Qiang Xue

Finished js validation for number validator.

parent 4e7a33bc
...@@ -86,7 +86,7 @@ yii.validation = (function ($) { ...@@ -86,7 +86,7 @@ yii.validation = (function ($) {
return; return;
} }
if (!value.match(options.pattern)) { if (!options.not && !value.match(options.pattern) || options.not && value.match(options.pattern)) {
messages.push(options.message) messages.push(options.message)
} }
}, },
...@@ -134,6 +134,24 @@ yii.validation = (function ($) { ...@@ -134,6 +134,24 @@ yii.validation = (function ($) {
if (options.is !== undefined && value.length != options.is) { if (options.is !== undefined && value.length != options.is) {
messages.push(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); })(jQuery);
...@@ -8,6 +8,9 @@ ...@@ -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;
/** /**
* NumberValidator validates that the attribute value is a number. * NumberValidator validates that the attribute value is a number.
...@@ -116,48 +119,36 @@ class NumberValidator extends Validator ...@@ -116,48 +119,36 @@ class NumberValidator extends Validator
public function clientValidateAttribute($object, $attribute) public function clientValidateAttribute($object, $attribute)
{ {
$label = $object->getAttributeLabel($attribute); $label = $object->getAttributeLabel($attribute);
$message = strtr($this->message, array( $value = $object->$attribute;
'{attribute}' => $label,
)); $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) { if ($this->min !== null) {
$tooSmall = strtr($this->tooSmall, array( $options['min'] = $this->min;
$options['tooSmall'] = Html::encode(strtr($this->tooSmall, array(
'{attribute}' => $label, '{attribute}' => $label,
'{value}' => $value,
'{min}' => $this->min, '{min}' => $this->min,
)); )));
$js .= "
if(value<{$this->min}) {
messages.push(" . json_encode($tooSmall) . ");
}
";
} }
if ($this->max !== null) { if ($this->max !== null) {
$tooBig = strtr($this->tooBig, array( $options['max'] = $this->max;
$options['tooBig'] = Html::encode(strtr($this->tooBig, array(
'{attribute}' => $label, '{attribute}' => $label,
'{value}' => $value,
'{max}' => $this->max, '{max}' => $this->max,
)); )));
$js .= "
if(value>{$this->max}) {
messages.push(" . json_encode($tooBig) . ");
}
";
} }
if ($this->skipOnEmpty) { if ($this->skipOnEmpty) {
$js = " $options['skipOnEmpty'] = 1;
if(jQuery.trim(value)!='') {
$js
}
";
} }
return $js; return 'yii.validation.number(value, messages, ' . Json::encode($options) . ');';
} }
} }
\ No newline at end of file
...@@ -100,6 +100,7 @@ class RegularExpressionValidator extends Validator ...@@ -100,6 +100,7 @@ class RegularExpressionValidator extends Validator
$options = array( $options = array(
'pattern' => new JsExpression($pattern), 'pattern' => new JsExpression($pattern),
'not' => $this->not,
'message' => Html::encode(strtr($this->message, array( 'message' => Html::encode(strtr($this->message, array(
'{attribute}' => $object->getAttributeLabel($attribute), '{attribute}' => $object->getAttributeLabel($attribute),
'{value}' => $object->$attribute, '{value}' => $object->$attribute,
......
...@@ -133,41 +133,36 @@ class StringValidator extends Validator ...@@ -133,41 +133,36 @@ class StringValidator extends Validator
$label = $object->getAttributeLabel($attribute); $label = $object->getAttributeLabel($attribute);
$value = $object->$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( $options = array(
'message' => Html::encode($message), 'message' => Html::encode(strtr($this->message, array(
'notEqual' => Html::encode($notEqual), '{attribute}' => $label,
'tooShort' => Html::encode($tooShort), '{value}' => $value,
'tooLong' => Html::encode($tooLong), ))),
); );
if ($this->min !== null) { if ($this->min !== null) {
$options['min'] = $this->min; $options['min'] = $this->min;
$options['tooShort'] = Html::encode(strtr($this->tooShort, array(
'{attribute}' => $label,
'{value}' => $value,
'{min}' => $this->min,
)));
} }
if ($this->max !== null) { if ($this->max !== null) {
$options['max'] = $this->max; $options['max'] = $this->max;
$options['tooLong'] = Html::encode(strtr($this->tooLong, array(
'{attribute}' => $label,
'{value}' => $value,
'{max}' => $this->max,
)));
} }
if ($this->is !== null) { if ($this->is !== null) {
$options['is'] = $this->is; $options['is'] = $this->is;
$options['notEqual'] = Html::encode(strtr($this->notEqual, array(
'{attribute}' => $label,
'{value}' => $value,
'{length}' => $this->is,
)));
} }
if ($this->skipOnEmpty) { if ($this->skipOnEmpty) {
$options['skipOnEmpty'] = 1; $options['skipOnEmpty'] = 1;
......
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