Skip to content

Commit

Permalink
Add support to other_than numericality validator
Browse files Browse the repository at this point in the history
  • Loading branch information
tagliala committed Sep 28, 2021
1 parent 8a114e3 commit 9bf6d4d
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

## main / unreleased

* [FEATURE] Add support to `other_than` numericality validator
* [FEATURE] Drop Ruby 2.4 support
* [FEATURE] Drop Rails 5.0 and 5.1 support
* [ENHANCEMENT] Minor JS Refactor
Expand Down
3 changes: 3 additions & 0 deletions dist/client-side-validations.esm.js
Original file line number Diff line number Diff line change
Expand Up @@ -357,6 +357,9 @@ var VALIDATIONS$1 = {
},
odd: function odd(a) {
return parseInt(a, 10) % 2 === 1;
},
other_than: function other_than(a, b) {
return parseFloat(a) !== parseFloat(b);
}
};

Expand Down
3 changes: 3 additions & 0 deletions dist/client-side-validations.js
Original file line number Diff line number Diff line change
Expand Up @@ -365,6 +365,9 @@
},
odd: function odd(a) {
return parseInt(a, 10) % 2 === 1;
},
other_than: function other_than(a, b) {
return parseFloat(a) !== parseFloat(b);
}
};

Expand Down
3 changes: 3 additions & 0 deletions src/validators/local/numericality.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ const VALIDATIONS = {
},
odd: (a) => {
return parseInt(a, 10) % 2 === 1
},
other_than: (a, b) => {
return parseFloat(a) !== parseFloat(b)
}
}

Expand Down
14 changes: 14 additions & 0 deletions test/javascript/public/test/validators/numericality.js
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,20 @@ QUnit.test('when only allowing even values and the value is odd', function (asse
assert.equal(ClientSideValidations.validators.local.numericality(element, options), 'failed validation')
})

QUnit.test('when only allowing values other than 10 and value is 11', function (assert) {
var element = $('#form input')
var options = { messages: { other_than: 'failed validation', numericality: 'failed validation' }, other_than: 10 }
element.val('11')
assert.equal(ClientSideValidations.validators.local.numericality(element, options), undefined)
})

QUnit.test('when only allowing values other than 10 and value is 10', function (assert) {
var element = $('#form input')
var options = { messages: { other_than: 'failed validation', numericality: 'failed validation' }, other_than: 10 }
element.val('10')
assert.equal(ClientSideValidations.validators.local.numericality(element, options), 'failed validation')
})

QUnit.test('when value refers to another present input', function (assert) {
var form = $('#form')
var element1 = $('<input type="text" name="points_1" />')
Expand Down
3 changes: 3 additions & 0 deletions vendor/assets/javascripts/rails.validations.js
Original file line number Diff line number Diff line change
Expand Up @@ -365,6 +365,9 @@
},
odd: function odd(a) {
return parseInt(a, 10) % 2 === 1;
},
other_than: function other_than(a, b) {
return parseFloat(a) !== parseFloat(b);
}
};

Expand Down

0 comments on commit 9bf6d4d

Please sign in to comment.