Skip to content

Commit

Permalink
netteForms: min/max/range can compare strings
Browse files Browse the repository at this point in the history
  • Loading branch information
dg committed Nov 1, 2023
1 parent e5aed91 commit a32d0da
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 6 deletions.
17 changes: 13 additions & 4 deletions src/assets/netteForms.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
6 changes: 4 additions & 2 deletions tests/netteForms/spec/Nette.validateRuleSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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);
});

Expand Down
45 changes: 45 additions & 0 deletions tests/netteForms/spec/Nette.validatorsSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit a32d0da

Please sign in to comment.