|
|
@ -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 |
|
|
|
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; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
$options = array( |
|
|
|
|
|
|
|
'pattern' => new JsExpression($this->integerOnly ? $this->integerPattern : $this->numberPattern), |
|
|
|
|
|
|
|
'message' => Html::encode(strtr($this->message, array( |
|
|
|
'{attribute}' => $label, |
|
|
|
'{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) . ');'; |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |