Fix #19735: Fix yii\validators\NumberValidator to use programmable message for the value validation

This commit is contained in:
Bizley
2023-01-13 07:57:03 +01:00
committed by GitHub
parent 55ea8eee1e
commit 581a7b2543
3 changed files with 185 additions and 67 deletions

View File

@ -15,6 +15,7 @@ Yii Framework 2 Change Log
- Chg #19696: Change visibility of `yii\web\View::isPageEnded` to `protected` (lubosdz, samdark)
- Bug #19712: Cast shell_exec() output to string for jsCompressor (impayru)
- Bug #19731: Fix `yii\data\Sort` to generate proper link when multisort is on and attribute has a default sort order set (bizley)
- Bug #19735: Fix `yii\validators\NumberValidator` to use programmable message for the value validation (bizley)
2.0.47 November 18, 2022
------------------------

View File

@ -116,19 +116,19 @@ class NumberValidator extends Validator
protected function validateValue($value)
{
if (is_array($value) && !$this->allowArray) {
return [Yii::t('yii', '{attribute} is invalid.'), []];
return [$this->message, []];
}
$values = !is_array($value) ? [$value] : $value;
foreach ($values as $value) {
if ($this->isNotNumber($value)) {
return [Yii::t('yii', '{attribute} is invalid.'), []];
foreach ($values as $sample) {
if ($this->isNotNumber($sample)) {
return [$this->message, []];
}
$pattern = $this->integerOnly ? $this->integerPattern : $this->numberPattern;
if (!preg_match($pattern, StringHelper::normalizeNumber($value))) {
if (!preg_match($pattern, StringHelper::normalizeNumber($sample))) {
return [$this->message, []];
} elseif ($this->min !== null && $value < $this->min) {
} elseif ($this->min !== null && $sample < $this->min) {
return [$this->tooSmall, ['min' => $this->min]];
} elseif ($this->max !== null && $value > $this->max) {
} elseif ($this->max !== null && $sample > $this->max) {
return [$this->tooBig, ['max' => $this->max]];
}
}