Skip to content

Commit 581a7b2

Browse files
author
Bizley
authored
Fix #19735: Fix yii\validators\NumberValidator to use programmable message for the value validation
1 parent 55ea8ee commit 581a7b2

File tree

3 files changed

+185
-67
lines changed

3 files changed

+185
-67
lines changed

framework/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ Yii Framework 2 Change Log
1515
- Chg #19696: Change visibility of `yii\web\View::isPageEnded` to `protected` (lubosdz, samdark)
1616
- Bug #19712: Cast shell_exec() output to string for jsCompressor (impayru)
1717
- Bug #19731: Fix `yii\data\Sort` to generate proper link when multisort is on and attribute has a default sort order set (bizley)
18+
- Bug #19735: Fix `yii\validators\NumberValidator` to use programmable message for the value validation (bizley)
1819

1920
2.0.47 November 18, 2022
2021
------------------------

framework/validators/NumberValidator.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -116,19 +116,19 @@ public function validateAttribute($model, $attribute)
116116
protected function validateValue($value)
117117
{
118118
if (is_array($value) && !$this->allowArray) {
119-
return [Yii::t('yii', '{attribute} is invalid.'), []];
119+
return [$this->message, []];
120120
}
121121
$values = !is_array($value) ? [$value] : $value;
122-
foreach ($values as $value) {
123-
if ($this->isNotNumber($value)) {
124-
return [Yii::t('yii', '{attribute} is invalid.'), []];
122+
foreach ($values as $sample) {
123+
if ($this->isNotNumber($sample)) {
124+
return [$this->message, []];
125125
}
126126
$pattern = $this->integerOnly ? $this->integerPattern : $this->numberPattern;
127-
if (!preg_match($pattern, StringHelper::normalizeNumber($value))) {
127+
if (!preg_match($pattern, StringHelper::normalizeNumber($sample))) {
128128
return [$this->message, []];
129-
} elseif ($this->min !== null && $value < $this->min) {
129+
} elseif ($this->min !== null && $sample < $this->min) {
130130
return [$this->tooSmall, ['min' => $this->min]];
131-
} elseif ($this->max !== null && $value > $this->max) {
131+
} elseif ($this->max !== null && $sample > $this->max) {
132132
return [$this->tooBig, ['max' => $this->max]];
133133
}
134134
}

0 commit comments

Comments
 (0)