Fixes #10488: Fixed incorrect behavior of yii\validation\NumberValidator when used with locales where decimal separator is comma

This commit is contained in:
Alexander Makarov
2016-12-02 00:04:30 +03:00
parent dd94ead358
commit b17dfa03a2
3 changed files with 16 additions and 11 deletions

View File

@@ -8,6 +8,7 @@ Yii Framework 2 Change Log
- Bug #9305: Fixed MSSQL `Schema::TYPE_TIMESTAMP` to be 'datetime' instead of 'timestamp', which is just an incremental number (nkovacs)
- Bug #9616: Fixed mysql\Schema::loadColumnSchema to set enumValues attribute correctly if enum definition contains commas (fphammerle)
- Bug #9796: Initialization of not existing `yii\grid\ActionColumn` default buttons (arogachev)
- Bug #10488: Fixed incorrect behavior of `yii\validation\NumberValidator` when used with locales where decimal separator is comma (quantum13, samdark)
- Bug #12681: Changed `data` column type from `text` to `blob` to handle null-byte (`\0`) in serialized RBAC rule properly (silverfire)
- Bug #12714: Fixed `yii\validation\EmailValidator` to prevent false-positives checks when property `checkDns` is set to `true` (silverfire)
- Bug #12791: Fixed `yii\behaviors\AttributeTypecastBehavior` unable to automatically detect `attributeTypes`, triggering PHP Fatal Error (klimov-paul)

View File

@@ -120,17 +120,21 @@ class NumberValidator extends Validator
/**
* Returns string represenation of number value with replaced commas to dots, if decimal point
* of current locale is comma
* @param $value
* @param int|float|string $value
* @return string
*/
private function getStringValue($value)
{
$value = (string)$value;
$localeInfo = localeconv();
if (isset($localeInfo['decimal_point']) && $localeInfo['decimal_point'] ==',') {
return str_replace(',', '.', "$value");
} else {
return "$value";
$decimalPointSeparator = isset($localeInfo['decimal_point']) ? $localeInfo['decimal_point'] : null;
if ($decimalPointSeparator !== null && $decimalPointSeparator !== '.') {
$value = str_replace($decimalPointSeparator, '.', $value);
}
return $value;
}
/**

View File

@@ -39,9 +39,9 @@ class NumberValidatorTest extends TestCase
$this->assertTrue($val->validate(25.45));
$oldLocale = setlocale(LC_ALL, "0");
setlocale(LC_ALL, "en_US.UTF-8");
setlocale(LC_ALL, 'en_US.UTF-8', 'English_United States.1252');
$this->assertFalse($val->validate('25,45'));
setlocale(LC_ALL, "en_DK.UTF-8"); //decimal point is comma
setlocale(LC_ALL, 'da_DK.UTF-8', 'Danish_Denmark.1252'); //decimal point is comma
$this->assertTrue($val->validate('25,45'));
setlocale(LC_ALL, $oldLocale);
@@ -83,10 +83,10 @@ class NumberValidatorTest extends TestCase
$oldLocale = setlocale(LC_ALL, "0");
setlocale(LC_ALL, "en_US.UTF-8");
setlocale(LC_ALL, 'en_US.UTF-8', 'English_United States.1252');
$this->assertTrue($val->validate(.5));
setlocale(LC_ALL, "en_DK.UTF-8"); //decimal point is comma
setlocale(LC_ALL, 'da_DK.UTF-8', 'Danish_Denmark.1252'); //decimal point is comma
$this->assertTrue($val->validate(.5));
setlocale(LC_ALL, $oldLocale);
@@ -189,11 +189,11 @@ class NumberValidatorTest extends TestCase
$oldLocale = setlocale(LC_ALL, "0");
setlocale(LC_ALL, "en_US.UTF-8");
setlocale(LC_ALL, 'en_US.UTF-8', 'English_United States.1252');
$val->validateAttribute($model, 'attr_number');
$this->assertFalse($model->hasErrors('attr_number'));
setlocale(LC_ALL, "en_DK.UTF-8"); //decimal point is comma
setlocale(LC_ALL, 'da_DK.UTF-8', 'Danish_Denmark.1252'); //decimal point is comma
$val->validateAttribute($model, 'attr_number');
$this->assertFalse($model->hasErrors('attr_number'));