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 ...@@ -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 #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 #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 #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 #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 #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) - 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 ...@@ -128,14 +128,18 @@ class NumberValidator extends Validator
]; ];
if ($this->min !== null) { 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, [ $options['tooSmall'] = Yii::$app->getI18n()->format($this->tooSmall, [
'attribute' => $label, 'attribute' => $label,
'min' => $this->min, 'min' => $this->min,
], Yii::$app->language); ], Yii::$app->language);
} }
if ($this->max !== null) { 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, [ $options['tooBig'] = Yii::$app->getI18n()->format($this->tooBig, [
'attribute' => $label, 'attribute' => $label,
'max' => $this->max, 'max' => $this->max,
......
...@@ -6,7 +6,7 @@ use yii\validators\BooleanValidator; ...@@ -6,7 +6,7 @@ use yii\validators\BooleanValidator;
use yiiunit\TestCase; use yiiunit\TestCase;
/** /**
* BooleanValidatorTest * @group validators
*/ */
class BooleanValidatorTest extends TestCase class BooleanValidatorTest extends TestCase
{ {
......
...@@ -6,6 +6,9 @@ use yii\validators\CompareValidator; ...@@ -6,6 +6,9 @@ use yii\validators\CompareValidator;
use yiiunit\data\validators\models\FakedValidationModel; use yiiunit\data\validators\models\FakedValidationModel;
use yiiunit\TestCase; use yiiunit\TestCase;
/**
* @group validators
*/
class CompareValidatorTest extends TestCase class CompareValidatorTest extends TestCase
{ {
protected function setUp() protected function setUp()
......
...@@ -7,6 +7,9 @@ use yii\validators\DateValidator; ...@@ -7,6 +7,9 @@ use yii\validators\DateValidator;
use yiiunit\data\validators\models\FakedValidationModel; use yiiunit\data\validators\models\FakedValidationModel;
use yiiunit\TestCase; use yiiunit\TestCase;
/**
* @group validators
*/
class DateValidatorTest extends TestCase class DateValidatorTest extends TestCase
{ {
protected function setUp() protected function setUp()
......
...@@ -6,7 +6,7 @@ use yii\validators\DefaultValueValidator; ...@@ -6,7 +6,7 @@ use yii\validators\DefaultValueValidator;
use yiiunit\TestCase; use yiiunit\TestCase;
/** /**
* DefaultValueValidatorTest * @group validators
*/ */
class DefaultValueValidatorTest extends TestCase class DefaultValueValidatorTest extends TestCase
{ {
......
...@@ -6,7 +6,6 @@ use yiiunit\data\validators\models\FakedValidationModel; ...@@ -6,7 +6,6 @@ use yiiunit\data\validators\models\FakedValidationModel;
use yiiunit\TestCase; use yiiunit\TestCase;
/** /**
* EmailValidatorTest
* @group validators * @group validators
*/ */
class EmailValidatorTest extends TestCase class EmailValidatorTest extends TestCase
......
...@@ -3,6 +3,9 @@ namespace yiiunit\framework\validators\ExistValidatorDriverTests; ...@@ -3,6 +3,9 @@ namespace yiiunit\framework\validators\ExistValidatorDriverTests;
use yiiunit\framework\validators\ExistValidatorTest; use yiiunit\framework\validators\ExistValidatorTest;
/**
* @group validators
*/
class ExistValidatorPostgresTest extends ExistValidatorTest class ExistValidatorPostgresTest extends ExistValidatorTest
{ {
protected $driverName = 'pgsql'; protected $driverName = 'pgsql';
......
...@@ -3,6 +3,9 @@ namespace yiiunit\framework\validators\ExistValidatorDriverTests; ...@@ -3,6 +3,9 @@ namespace yiiunit\framework\validators\ExistValidatorDriverTests;
use yiiunit\framework\validators\ExistValidatorTest; use yiiunit\framework\validators\ExistValidatorTest;
/**
* @group validators
*/
class ExistValidatorSQliteTest extends ExistValidatorTest class ExistValidatorSQliteTest extends ExistValidatorTest
{ {
protected $driverName = 'sqlite'; protected $driverName = 'sqlite';
......
...@@ -12,6 +12,9 @@ use yiiunit\data\validators\models\ValidatorTestMainModel; ...@@ -12,6 +12,9 @@ use yiiunit\data\validators\models\ValidatorTestMainModel;
use yiiunit\data\validators\models\ValidatorTestRefModel; use yiiunit\data\validators\models\ValidatorTestRefModel;
use yiiunit\framework\db\DatabaseTestCase; use yiiunit\framework\db\DatabaseTestCase;
/**
* @group validators
*/
class ExistValidatorTest extends DatabaseTestCase class ExistValidatorTest extends DatabaseTestCase
{ {
protected $driverName = 'mysql'; protected $driverName = 'mysql';
......
...@@ -8,6 +8,9 @@ use Yii; ...@@ -8,6 +8,9 @@ use Yii;
use yiiunit\data\validators\models\FakedValidationModel; use yiiunit\data\validators\models\FakedValidationModel;
use yiiunit\TestCase; use yiiunit\TestCase;
/**
* @group validators
*/
class FileValidatorTest extends TestCase class FileValidatorTest extends TestCase
{ {
public function setUp() public function setUp()
......
...@@ -6,6 +6,9 @@ use yii\validators\FilterValidator; ...@@ -6,6 +6,9 @@ use yii\validators\FilterValidator;
use yiiunit\data\validators\models\FakedValidationModel; use yiiunit\data\validators\models\FakedValidationModel;
use yiiunit\TestCase; use yiiunit\TestCase;
/**
* @group validators
*/
class FilterValidatorTest extends TestCase class FilterValidatorTest extends TestCase
{ {
protected function setUp() protected function setUp()
......
...@@ -3,9 +3,13 @@ ...@@ -3,9 +3,13 @@
namespace yiiunit\framework\validators; namespace yiiunit\framework\validators;
use yii\validators\NumberValidator; use yii\validators\NumberValidator;
use yii\web\View;
use yiiunit\data\validators\models\FakedValidationModel; use yiiunit\data\validators\models\FakedValidationModel;
use yiiunit\TestCase; use yiiunit\TestCase;
/**
* @group validators
*/
class NumberValidatorTest extends TestCase class NumberValidatorTest extends TestCase
{ {
protected function setUp() protected function setUp()
...@@ -162,4 +166,46 @@ class NumberValidatorTest extends TestCase ...@@ -162,4 +166,46 @@ class NumberValidatorTest extends TestCase
$msgs = $model->getErrors('attr_number'); $msgs = $model->getErrors('attr_number');
$this->assertSame('attr_number is to small.', $msgs[0]); $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; ...@@ -6,6 +6,9 @@ use yii\validators\RangeValidator;
use yiiunit\data\validators\models\FakedValidationModel; use yiiunit\data\validators\models\FakedValidationModel;
use yiiunit\TestCase; use yiiunit\TestCase;
/**
* @group validators
*/
class RangeValidatorTest extends TestCase class RangeValidatorTest extends TestCase
{ {
protected function setUp() protected function setUp()
......
...@@ -6,6 +6,9 @@ use yii\validators\RegularExpressionValidator; ...@@ -6,6 +6,9 @@ use yii\validators\RegularExpressionValidator;
use yiiunit\data\validators\models\FakedValidationModel; use yiiunit\data\validators\models\FakedValidationModel;
use yiiunit\TestCase; use yiiunit\TestCase;
/**
* @group validators
*/
class RegularExpressionValidatorTest extends TestCase class RegularExpressionValidatorTest extends TestCase
{ {
protected function setUp() protected function setUp()
......
...@@ -5,6 +5,9 @@ use yii\validators\RequiredValidator; ...@@ -5,6 +5,9 @@ use yii\validators\RequiredValidator;
use yiiunit\data\validators\models\FakedValidationModel; use yiiunit\data\validators\models\FakedValidationModel;
use yiiunit\TestCase; use yiiunit\TestCase;
/**
* @group validators
*/
class RequiredValidatorTest extends TestCase class RequiredValidatorTest extends TestCase
{ {
protected function setUp() protected function setUp()
......
...@@ -6,6 +6,9 @@ use yii\validators\StringValidator; ...@@ -6,6 +6,9 @@ use yii\validators\StringValidator;
use yiiunit\data\validators\models\FakedValidationModel; use yiiunit\data\validators\models\FakedValidationModel;
use yiiunit\TestCase; use yiiunit\TestCase;
/**
* @group validators
*/
class StringValidatorTest extends TestCase class StringValidatorTest extends TestCase
{ {
public function setUp() public function setUp()
......
...@@ -4,6 +4,9 @@ namespace yiiunit\framework\validators\UniqueValidatorDriverTests; ...@@ -4,6 +4,9 @@ namespace yiiunit\framework\validators\UniqueValidatorDriverTests;
use yiiunit\framework\validators\UniqueValidatorTest; use yiiunit\framework\validators\UniqueValidatorTest;
/**
* @group validators
*/
class UniqueValidatorPostgresTest extends UniqueValidatorTest class UniqueValidatorPostgresTest extends UniqueValidatorTest
{ {
protected $driverName = 'pgsql'; protected $driverName = 'pgsql';
......
...@@ -4,6 +4,9 @@ namespace yiiunit\framework\validators\UniqueValidatorDriverTests; ...@@ -4,6 +4,9 @@ namespace yiiunit\framework\validators\UniqueValidatorDriverTests;
use yiiunit\framework\validators\UniqueValidatorTest; use yiiunit\framework\validators\UniqueValidatorTest;
/**
* @group validators
*/
class UniqueValidatorSQliteTest extends UniqueValidatorTest class UniqueValidatorSQliteTest extends UniqueValidatorTest
{ {
protected $driverName = 'sqlite'; protected $driverName = 'sqlite';
......
...@@ -12,6 +12,9 @@ use yiiunit\data\validators\models\ValidatorTestMainModel; ...@@ -12,6 +12,9 @@ use yiiunit\data\validators\models\ValidatorTestMainModel;
use yiiunit\data\validators\models\ValidatorTestRefModel; use yiiunit\data\validators\models\ValidatorTestRefModel;
use yiiunit\framework\db\DatabaseTestCase; use yiiunit\framework\db\DatabaseTestCase;
/**
* @group validators
*/
class UniqueValidatorTest extends DatabaseTestCase class UniqueValidatorTest extends DatabaseTestCase
{ {
protected $driverName = 'mysql'; protected $driverName = 'mysql';
......
...@@ -7,7 +7,7 @@ use yii\validators\UrlValidator; ...@@ -7,7 +7,7 @@ use yii\validators\UrlValidator;
use yiiunit\TestCase; use yiiunit\TestCase;
/** /**
* UrlValidatorTest * @group validators
*/ */
class UrlValidatorTest extends TestCase class UrlValidatorTest extends TestCase
{ {
......
...@@ -9,6 +9,9 @@ use yiiunit\data\validators\models\FakedValidationModel; ...@@ -9,6 +9,9 @@ use yiiunit\data\validators\models\FakedValidationModel;
use yiiunit\data\validators\TestValidator; use yiiunit\data\validators\TestValidator;
use yiiunit\TestCase; use yiiunit\TestCase;
/**
* @group validators
*/
class ValidatorTest extends TestCase class ValidatorTest extends TestCase
{ {
protected function setUp() 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