diff --git a/src/assets/netteForms.js b/src/assets/netteForms.js index c91c3cf91..13e7c41e6 100644 --- a/src/assets/netteForms.js +++ b/src/assets/netteForms.js @@ -492,16 +492,25 @@ }, min: function(elem, arg, val) { - return arg === null || parseFloat(val) >= arg; + if (Number.isFinite(arg)) { + val = parseFloat(val); + } + return val >= arg; }, max: function(elem, arg, val) { - return arg === null || parseFloat(val) <= arg; + if (Number.isFinite(arg)) { + val = parseFloat(val); + } + return val <= arg; }, range: function(elem, arg, val) { - return Array.isArray(arg) ? - ((arg[0] === null || parseFloat(val) >= arg[0]) && (arg[1] === null || parseFloat(val) <= arg[1])) : null; + if (!Array.isArray(arg)) { + return null; + } + return (arg[0] === null || Nette.validators.min(elem, arg[0], val)) + && (arg[1] === null || Nette.validators.max(elem, arg[1], val)); }, submitted: function(elem) { diff --git a/tests/netteForms/spec/Nette.validateRuleSpec.js b/tests/netteForms/spec/Nette.validateRuleSpec.js index 84ade96a6..4fc4ec33c 100644 --- a/tests/netteForms/spec/Nette.validateRuleSpec.js +++ b/tests/netteForms/spec/Nette.validateRuleSpec.js @@ -64,7 +64,8 @@ describe('Nette.getValue & validateRule', function() { expect(Nette.validateRule(el, 'min', -1000)).toBe(false); expect(Nette.validateRule(el, 'max', -2000)).toBe(false); expect(Nette.validateRule(el, 'max', -1000)).toBe(true); - expect(Nette.validateRule(el, 'range', ['-2000', '-1000'])).toBe(true); + expect(Nette.validateRule(el, 'range', [-2000, -1000])).toBe(true); + expect(Nette.validateRule(el, 'range', ['-1200', '-1300'])).toBe(true); expect(Nette.validateRule(el, 'range', [10, null])).toBe(false); el.value = '-12.5'; @@ -74,7 +75,8 @@ describe('Nette.getValue & validateRule', function() { expect(Nette.validateRule(el, 'min', -10)).toBe(false); expect(Nette.validateRule(el, 'max', -2000)).toBe(false); expect(Nette.validateRule(el, 'max', -10)).toBe(true); - expect(Nette.validateRule(el, 'range', ['-12.6', '-12.4'])).toBe(true); + expect(Nette.validateRule(el, 'range', [-12.6, -12.4])).toBe(true); + expect(Nette.validateRule(el, 'range', ['-12.4', '-12.6'])).toBe(true); expect(Nette.validateRule(el, 'range', [-5, 10])).toBe(false); }); diff --git a/tests/netteForms/spec/Nette.validatorsSpec.js b/tests/netteForms/spec/Nette.validatorsSpec.js index 187581130..555b5befc 100644 --- a/tests/netteForms/spec/Nette.validatorsSpec.js +++ b/tests/netteForms/spec/Nette.validatorsSpec.js @@ -22,6 +22,51 @@ describe('Nette.validators', function() { }); + it('min', function() { + expect(Nette.validators.min(null, 0, '')).toBe(false); + expect(Nette.validators.min(null, 0, 'foo')).toBe(false); + expect(Nette.validators.min(null, 0, '0')).toBe(true); + expect(Nette.validators.min(null, 0, '1')).toBe(true); + expect(Nette.validators.min(null, 0, '-1')).toBe(false); + expect(Nette.validators.min(null, 0, 0)).toBe(true); + expect(Nette.validators.min(null, 0, 1)).toBe(true); + expect(Nette.validators.min(null, 0, -1)).toBe(false); + expect(Nette.validators.min(null, '2023-10-29', '2023-10-30')).toBe(true); + expect(Nette.validators.min(null, '2023-10-29', '2023-10-28')).toBe(false); + }); + + + it('max', function() { + expect(Nette.validators.max(null, 0, '')).toBe(false); + expect(Nette.validators.max(null, 0, 'foo')).toBe(false); + expect(Nette.validators.max(null, 0, '0')).toBe(true); + expect(Nette.validators.max(null, 0, '1')).toBe(false); + expect(Nette.validators.max(null, 0, '-1')).toBe(true); + expect(Nette.validators.max(null, 0, 0)).toBe(true); + expect(Nette.validators.max(null, 0, 1)).toBe(false); + expect(Nette.validators.max(null, 0, -1)).toBe(true); + expect(Nette.validators.max(null, '2023-10-29', '2023-10-30')).toBe(false); + expect(Nette.validators.max(null, '2023-10-29', '2023-10-28')).toBe(true); + }); + + + it('range', function() { + let el = document.createElement('input'); + + expect(Nette.validators.range(el, null, 0)).toBe(null); + expect(Nette.validators.range(el, 'foo', 0)).toBe(null); + expect(Nette.validators.range(el, ['0', null], 0)).toBe(true); + expect(Nette.validators.range(el, ['1', null], 0)).toBe(false); + expect(Nette.validators.range(el, [-1, 1], 0)).toBe(true); + expect(Nette.validators.range(el, ['2023-10-29', '2023-10-31'], '2023-10-30')).toBe(true); + expect(Nette.validators.range(el, ['2023-10-29', '2023-10-31'], '2023-10-28')).toBe(false); + expect(Nette.validators.range(el, [null, '1'], 0)).toBe(true); + expect(Nette.validators.range(el, ['10:30', '14:00'], '12:30')).toBe(true); + expect(Nette.validators.range(el, ['10:30', '14:00'], '09:30')).toBe(false); + expect(Nette.validators.range(el, ['14:00', '10:30'], '12:30')).toBe(false); + }); + + it('email', function() { expect(Nette.validators.email(null, null, '')).toBe(false); expect(Nette.validators.email(null, null, 'hello')).toBe(false);