From dfe828f76bccdd00aae0e245ec0f2e5ad7f38c77 Mon Sep 17 00:00:00 2001 From: Evgeniy Moiseenko Date: Fri, 19 Oct 2018 23:56:30 +0300 Subject: [PATCH] Fixes #13932: Fix number validator attributes comparison --- framework/CHANGELOG.md | 1 + framework/assets/yii.validation.js | 6 ++--- tests/js/tests/yii.validation.test.js | 35 +++++++++++++++++++++++++++ 3 files changed, 39 insertions(+), 3 deletions(-) diff --git a/framework/CHANGELOG.md b/framework/CHANGELOG.md index 6b25db9ed7..cf4d641447 100644 --- a/framework/CHANGELOG.md +++ b/framework/CHANGELOG.md @@ -4,6 +4,7 @@ Yii Framework 2 Change Log 2.0.16 under development ------------------------ +- Bug #13932: Fix number validator attributes comparison (uaoleg, s1lver) - Bug #14039, #16636: Fixed validation for disabled inputs (s1lver, omzy83) - Bug #16425: Check for additional values for disabled confirm dialog (Alex-Code, s1lver) - Enh #14367: In `yii\db\mysql\QueryBuilder` added support fractional seconds for time types for MySQL >= 5.6.4 (konstantin-vl) diff --git a/framework/assets/yii.validation.js b/framework/assets/yii.validation.js index 16a74f03ca..81cf69c222 100644 --- a/framework/assets/yii.validation.js +++ b/framework/assets/yii.validation.js @@ -282,12 +282,12 @@ yii.validation = (function ($) { if (!$target.length) { $target = $form.find('[name="' + options.compareAttributeName + '"]'); } - compareValue = $target.val(); + compareValue = $target.val(); } if (options.type === 'number') { - value = parseFloat(value); - compareValue = parseFloat(compareValue); + value = value ? parseFloat(value) : 0; + compareValue = compareValue ? parseFloat(compareValue) : 0; } switch (options.operator) { case '==': diff --git a/tests/js/tests/yii.validation.test.js b/tests/js/tests/yii.validation.test.js index ea8ffb3651..e5196d309b 100644 --- a/tests/js/tests/yii.validation.test.js +++ b/tests/js/tests/yii.validation.test.js @@ -1518,6 +1518,41 @@ describe('yii.validation', function () { {operator: '<', compareValue: '2', type: 'number'}, false ], + 'number type, ">=" operator, 2nd is lower': [ + 10, + {operator: '>=', compareValue: 2, type: 'number'}, + true + ], + 'number type, "<=" operator, 2nd is lower': [ + 10, + {operator: '<=', compareValue: 2, type: 'number'}, + false + ], + 'number type, ">" operator, 2nd is lower': [ + 10, + {operator: '>', compareValue: 2, type: 'number'}, + true + ], + 'number type, ">" operator, compare value undefined': [ + undefined, + {operator: '>', compareValue: 2, type: 'number'}, + false + ], + 'number type, "<" operator, compare value undefined': [ + undefined, + {operator: '<', compareValue: 2, type: 'number'}, + true + ], + 'number type, ">=" operator, compare value undefined': [ + undefined, + {operator: '>=', compareValue: 2, type: 'number'}, + false + ], + 'number type, "<=" operator, compare value undefined': [ + undefined, + {operator: '<=', compareValue: 2, type: 'number'}, + true + ], // default compare value 'default compare value, "===" operator, against undefined': [undefined, {operator: '==='}, true] }, function (value, options, expectValid) {