Fixes #15553: Fixed yii\validators\NumberValidator incorrectly validate resource

This commit is contained in:
Elvira Sheina
2018-01-25 16:28:41 +05:00
committed by Alexander Makarov
parent dbcb66b87a
commit eb57d4191c
3 changed files with 54 additions and 2 deletions

View File

@@ -81,7 +81,7 @@ class NumberValidator extends Validator
public function validateAttribute($model, $attribute)
{
$value = $model->$attribute;
if (is_array($value) || (is_object($value) && !method_exists($value, '__toString'))) {
if ($this->isNotNumber($value)) {
$this->addError($model, $attribute, $this->message);
return;
}
@@ -103,7 +103,7 @@ class NumberValidator extends Validator
*/
protected function validateValue($value)
{
if (is_array($value) || is_object($value)) {
if ($this->isNotNumber($value)) {
return [Yii::t('yii', '{attribute} is invalid.'), []];
}
$pattern = $this->integerOnly ? $this->integerPattern : $this->numberPattern;
@@ -118,6 +118,16 @@ class NumberValidator extends Validator
return null;
}
/*
* @param mixed $value the data value to be checked.
*/
private function isNotNumber($value)
{
return is_array($value)
|| (is_object($value) && !method_exists($value, '__toString'))
|| (!is_object($value) && !is_scalar($value) && !is_null($value));
}
/**
* {@inheritdoc}
*/