Skip to content
This repository has been archived by the owner on Dec 19, 2024. It is now read-only.

validate method supports multiple validators in validatable #90

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion bower.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
"ignore": [],
"dependencies": {
"iron-a11y-announcer": "PolymerElements/iron-a11y-announcer#^1.0.0",
"iron-validatable-behavior": "PolymerElements/iron-validatable-behavior#^1.0.0",
"iron-validatable-behavior": "https://github.com/tlouisse/iron-validatable-behavior.git#feature/multipleValidators",
"polymer": "Polymer/polymer#^1.0.0"
},
"devDependencies": {
Expand All @@ -31,5 +31,9 @@
"test-fixture": "PolymerElements/test-fixture#^1.0.0",
"web-component-tester": "^4.0.0",
"webcomponentsjs": "webcomponents/webcomponentsjs#^0.7.0"
},
"resolutions": {
"iron-validatable-behavior": "feature/multipleValidators",
"iron-validator-behavior": "feature/multipleValidators"
}
}
19 changes: 5 additions & 14 deletions iron-input.html
Original file line number Diff line number Diff line change
Expand Up @@ -278,20 +278,11 @@
* @return {boolean} True if the value is valid.
*/
validate: function() {
// First, check what the browser thinks. Some inputs (like type=number)
// behave weirdly and will set the value to "" if something invalid is
// entered, but will set the validity correctly.
var valid = this.checkValidity();

// Only do extra checking if the browser thought this was valid.
if (valid) {
// Empty, required input is invalid
if (this.required && this.value === '') {
valid = false;
} else if (this.hasValidator()) {
valid = Polymer.IronValidatableBehavior.validate.call(this, this.value);
}
}
// Validatable is called first, so it will keep track of all individual states of validators that can be applied to it.
var validatableOutcome = Polymer.IronValidatableBehavior.validate.call(this, this.value);

var requiredValid = this.required ? this.value !== '' : true;
var valid = validatableOutcome && this.checkValidity() && requiredValid;

this.invalid = !valid;
this.fire('iron-input-validate');
Expand Down
12 changes: 12 additions & 0 deletions test/iron-input.html
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,18 @@
input.validate();
assert.isTrue(input.invalid, 'input is invalid');
});

test('custom validators will be called when browser validation results in invalid state'/* this enables error reporting per validator */, function() {
var node = fixture('native-and-custom-validator');
var validator = node[0];
var input = node[1];
sinon.spy(validator, 'validate');
// The validator allows this, but the browser doesn't.
input.value = 'zzz';
input.validate();
assert(validator.validate.called);
});

});

suite('a11y', function() {
Expand Down