Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Check the number field validation constraints inside checkValidity #443

Merged
merged 1 commit into from
Nov 14, 2019
Merged
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
4 changes: 2 additions & 2 deletions src/vaadin-number-field.html
Original file line number Diff line number Diff line change
Expand Up @@ -333,7 +333,7 @@
}

__onInputChange() {
this.checkValidity();
this.validate();
}

_stepOrMinChanged(step, min) {
Expand All @@ -344,7 +344,7 @@
checkValidity() {
// text-field mixin does not check against `min`, `max` and `step`
if (this.min !== undefined || this.max !== undefined || this.step !== 1) {
this.invalid = !this.inputElement.checkValidity();
return this.inputElement.checkValidity();
}
return super.checkValidity();
}
Expand Down
26 changes: 26 additions & 0 deletions test/number-field.html
Original file line number Diff line number Diff line change
Expand Up @@ -705,6 +705,32 @@
expect(numberField.validate()).to.be.true;
});

it('should align checkValidity with the native input element', () => {
numberField.value = -1;
numberField.min = 0;

expect(numberField.checkValidity()).to.equal(numberField.inputElement.checkValidity());
});

it('should not validate when explicitly set to invalid', () => {
numberField.invalid = true;

expect(numberField.value).to.be.empty;
expect(numberField.validate()).to.be.false;

expect(numberField.invalid).to.be.true;
});

it('should validate when inherited validation constraint is provided and explicitly set to invalid', () => {
numberField.invalid = true;
numberField.maxlength = 5;

expect(numberField.value).to.be.empty;
expect(numberField.validate()).to.be.true;

expect(numberField.invalid).to.be.false;
});

it('should allow setting decimals', () => {
numberField.value = 7.6;
expect(numberField.value).to.be.equal('7.6');
Expand Down