Commit 08c3f90d by Qiang Xue

Finished js validation for number validator.

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