Commit 83a63f23 by Carsten Brandt

fixed wrong client validation issue in numbervalidator

fixes #3118
parent 4f6e1d30
......@@ -11,6 +11,7 @@ Yii Framework 2 Change Log
- Bug #3042: `yii\widgets\Pjax` should end application right after it finishes responding to a pjax request (qiangxue)
- Bug #3066: `yii\db\mssql\Schema::getTableSchema()` should return null when the table does not exist (qiangxue)
- Bug #3091: Fixed inconsistent treatment of `Widget::run()` when a widget is used as a container and as a self-contained object (qiangxue)
- Bug #3118: Ensure client validation has the same behavior as server side validation for number validator (cebe)
- Bug #3121: `yii\base\Application::bootstrap` may fail to load some components if they are specified as class names (qiangxue)
- Bug #3125: `yii\console\controllers\AssetController` now respects data URL resources (klimov-paul)
- Bug #3128: Fixed the bug that `defaultRoles` set in RBAC manager was not working as specified (qiangxue)
......
......@@ -128,14 +128,18 @@ class NumberValidator extends Validator
];
if ($this->min !== null) {
$options['min'] = $this->min;
// ensure numeric value to make javascript comparison equal to PHP comparison
// https://github.com/yiisoft/yii2/issues/3118
$options['min'] = is_string($this->min) ? (float)$this->min : $this->min;
$options['tooSmall'] = Yii::$app->getI18n()->format($this->tooSmall, [
'attribute' => $label,
'min' => $this->min,
], Yii::$app->language);
}
if ($this->max !== null) {
$options['max'] = $this->max;
// ensure numeric value to make javascript comparison equal to PHP comparison
// https://github.com/yiisoft/yii2/issues/3118
$options['max'] = is_string($this->max) ? (float)$this->max : $this->max;
$options['tooBig'] = Yii::$app->getI18n()->format($this->tooBig, [
'attribute' => $label,
'max' => $this->max,
......
......@@ -6,7 +6,7 @@ use yii\validators\BooleanValidator;
use yiiunit\TestCase;
/**
* BooleanValidatorTest
* @group validators
*/
class BooleanValidatorTest extends TestCase
{
......
......@@ -6,6 +6,9 @@ use yii\validators\CompareValidator;
use yiiunit\data\validators\models\FakedValidationModel;
use yiiunit\TestCase;
/**
* @group validators
*/
class CompareValidatorTest extends TestCase
{
protected function setUp()
......
......@@ -7,6 +7,9 @@ use yii\validators\DateValidator;
use yiiunit\data\validators\models\FakedValidationModel;
use yiiunit\TestCase;
/**
* @group validators
*/
class DateValidatorTest extends TestCase
{
protected function setUp()
......
......@@ -6,7 +6,7 @@ use yii\validators\DefaultValueValidator;
use yiiunit\TestCase;
/**
* DefaultValueValidatorTest
* @group validators
*/
class DefaultValueValidatorTest extends TestCase
{
......
......@@ -6,7 +6,6 @@ use yiiunit\data\validators\models\FakedValidationModel;
use yiiunit\TestCase;
/**
* EmailValidatorTest
* @group validators
*/
class EmailValidatorTest extends TestCase
......
......@@ -3,6 +3,9 @@ namespace yiiunit\framework\validators\ExistValidatorDriverTests;
use yiiunit\framework\validators\ExistValidatorTest;
/**
* @group validators
*/
class ExistValidatorPostgresTest extends ExistValidatorTest
{
protected $driverName = 'pgsql';
......
......@@ -3,6 +3,9 @@ namespace yiiunit\framework\validators\ExistValidatorDriverTests;
use yiiunit\framework\validators\ExistValidatorTest;
/**
* @group validators
*/
class ExistValidatorSQliteTest extends ExistValidatorTest
{
protected $driverName = 'sqlite';
......
......@@ -12,6 +12,9 @@ use yiiunit\data\validators\models\ValidatorTestMainModel;
use yiiunit\data\validators\models\ValidatorTestRefModel;
use yiiunit\framework\db\DatabaseTestCase;
/**
* @group validators
*/
class ExistValidatorTest extends DatabaseTestCase
{
protected $driverName = 'mysql';
......
......@@ -8,6 +8,9 @@ use Yii;
use yiiunit\data\validators\models\FakedValidationModel;
use yiiunit\TestCase;
/**
* @group validators
*/
class FileValidatorTest extends TestCase
{
public function setUp()
......
......@@ -6,6 +6,9 @@ use yii\validators\FilterValidator;
use yiiunit\data\validators\models\FakedValidationModel;
use yiiunit\TestCase;
/**
* @group validators
*/
class FilterValidatorTest extends TestCase
{
protected function setUp()
......
......@@ -3,9 +3,13 @@
namespace yiiunit\framework\validators;
use yii\validators\NumberValidator;
use yii\web\View;
use yiiunit\data\validators\models\FakedValidationModel;
use yiiunit\TestCase;
/**
* @group validators
*/
class NumberValidatorTest extends TestCase
{
protected function setUp()
......@@ -162,4 +166,46 @@ class NumberValidatorTest extends TestCase
$msgs = $model->getErrors('attr_number');
$this->assertSame('attr_number is to small.', $msgs[0]);
}
/**
* https://github.com/yiisoft/yii2/issues/3118
*/
public function testClientValidateComparison()
{
$val = new NumberValidator([
'min' => 5,
'max' => 10,
]);
$model = new FakedValidationModel();
$js = $val->clientValidateAttribute($model, 'attr_number', new View(['assetBundles' => ['yii\validators\ValidationAsset' => true]]));
$this->assertContains('"min":5', $js);
$this->assertContains('"max":10', $js);
$val = new NumberValidator([
'min' => '5',
'max' => '10',
]);
$model = new FakedValidationModel();
$js = $val->clientValidateAttribute($model, 'attr_number', new View(['assetBundles' => ['yii\validators\ValidationAsset' => true]]));
$this->assertContains('"min":5', $js);
$this->assertContains('"max":10', $js);
$val = new NumberValidator([
'min' => 5.65,
'max' => 13.37,
]);
$model = new FakedValidationModel();
$js = $val->clientValidateAttribute($model, 'attr_number', new View(['assetBundles' => ['yii\validators\ValidationAsset' => true]]));
$this->assertContains('"min":5.65', $js);
$this->assertContains('"max":13.37', $js);
$val = new NumberValidator([
'min' => '5.65',
'max' => '13.37',
]);
$model = new FakedValidationModel();
$js = $val->clientValidateAttribute($model, 'attr_number', new View(['assetBundles' => ['yii\validators\ValidationAsset' => true]]));
$this->assertContains('"min":5.65', $js);
$this->assertContains('"max":13.37', $js);
}
}
......@@ -6,6 +6,9 @@ use yii\validators\RangeValidator;
use yiiunit\data\validators\models\FakedValidationModel;
use yiiunit\TestCase;
/**
* @group validators
*/
class RangeValidatorTest extends TestCase
{
protected function setUp()
......
......@@ -6,6 +6,9 @@ use yii\validators\RegularExpressionValidator;
use yiiunit\data\validators\models\FakedValidationModel;
use yiiunit\TestCase;
/**
* @group validators
*/
class RegularExpressionValidatorTest extends TestCase
{
protected function setUp()
......
......@@ -5,6 +5,9 @@ use yii\validators\RequiredValidator;
use yiiunit\data\validators\models\FakedValidationModel;
use yiiunit\TestCase;
/**
* @group validators
*/
class RequiredValidatorTest extends TestCase
{
protected function setUp()
......
......@@ -6,6 +6,9 @@ use yii\validators\StringValidator;
use yiiunit\data\validators\models\FakedValidationModel;
use yiiunit\TestCase;
/**
* @group validators
*/
class StringValidatorTest extends TestCase
{
public function setUp()
......
......@@ -4,6 +4,9 @@ namespace yiiunit\framework\validators\UniqueValidatorDriverTests;
use yiiunit\framework\validators\UniqueValidatorTest;
/**
* @group validators
*/
class UniqueValidatorPostgresTest extends UniqueValidatorTest
{
protected $driverName = 'pgsql';
......
......@@ -4,6 +4,9 @@ namespace yiiunit\framework\validators\UniqueValidatorDriverTests;
use yiiunit\framework\validators\UniqueValidatorTest;
/**
* @group validators
*/
class UniqueValidatorSQliteTest extends UniqueValidatorTest
{
protected $driverName = 'sqlite';
......
......@@ -12,6 +12,9 @@ use yiiunit\data\validators\models\ValidatorTestMainModel;
use yiiunit\data\validators\models\ValidatorTestRefModel;
use yiiunit\framework\db\DatabaseTestCase;
/**
* @group validators
*/
class UniqueValidatorTest extends DatabaseTestCase
{
protected $driverName = 'mysql';
......
......@@ -7,7 +7,7 @@ use yii\validators\UrlValidator;
use yiiunit\TestCase;
/**
* UrlValidatorTest
* @group validators
*/
class UrlValidatorTest extends TestCase
{
......
......@@ -9,6 +9,9 @@ use yiiunit\data\validators\models\FakedValidationModel;
use yiiunit\data\validators\TestValidator;
use yiiunit\TestCase;
/**
* @group validators
*/
class ValidatorTest extends TestCase
{
protected function setUp()
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment