diff --git a/framework/helpers/BaseStringHelper.php b/framework/helpers/BaseStringHelper.php index 48d5b1d8ac..1b8fd81b39 100644 --- a/framework/helpers/BaseStringHelper.php +++ b/framework/helpers/BaseStringHelper.php @@ -284,4 +284,25 @@ class BaseStringHelper { return count(preg_split('/\s+/u', $string, null, PREG_SPLIT_NO_EMPTY)); } + + /** + * Returns string represenation of number value with replaced commas to dots, if decimal point + * of current locale is comma + * @param int|float|string $value + * @return string + * @since 2.0.11 + */ + public static function normalizeNumber($value) + { + $value = "$value"; + + $localeInfo = localeconv(); + $decimalSeparator = isset($localeInfo['decimal_point']) ? $localeInfo['decimal_point'] : null; + + if ($decimalSeparator !== null && $decimalSeparator !== '.') { + $value = str_replace($decimalSeparator, '.', $value); + } + + return $value; + } } diff --git a/framework/validators/NumberValidator.php b/framework/validators/NumberValidator.php index 261b192bd1..4f8e017fa5 100644 --- a/framework/validators/NumberValidator.php +++ b/framework/validators/NumberValidator.php @@ -8,6 +8,7 @@ namespace yii\validators; use Yii; +use yii\helpers\StringHelper; use yii\web\JsExpression; use yii\helpers\Json; @@ -86,7 +87,7 @@ class NumberValidator extends Validator } $pattern = $this->integerOnly ? $this->integerPattern : $this->numberPattern; - if (!preg_match($pattern, $this->getStringValue($value))) { + if (!preg_match($pattern, StringHelper::normalizeNumber($value))) { $this->addError($model, $attribute, $this->message); } if ($this->min !== null && $value < $this->min) { @@ -106,7 +107,7 @@ class NumberValidator extends Validator return [Yii::t('yii', '{attribute} is invalid.'), []]; } $pattern = $this->integerOnly ? $this->integerPattern : $this->numberPattern; - if (!preg_match($pattern, $this->getStringValue($value))) { + if (!preg_match($pattern, StringHelper::normalizeNumber($value))) { return [$this->message, []]; } elseif ($this->min !== null && $value < $this->min) { return [$this->tooSmall, ['min' => $this->min]];