diff --git a/src/vaadin-date-picker-mixin.html b/src/vaadin-date-picker-mixin.html index 81ddf16e..34cc4f7c 100644 --- a/src/vaadin-date-picker-mixin.html +++ b/src/vaadin-date-picker-mixin.html @@ -818,6 +818,27 @@ } } + /** + * @param {boolean} invalid + * @protected + */ + _setInvalid(invalid) { + if (this._shouldSetInvalid(invalid)) { + this.invalid = invalid; + } + } + + /** + * Override this method to define whether the given `invalid` state should be set. + * + * @param {boolean} _invalid + * @return {boolean} + * @protected + */ + _shouldSetInvalid(_invalid) { + return true; + } + /** * Validates the field and sets the `invalid` property based on the result. * @@ -830,7 +851,7 @@ // to avoid breaking change on custom `checkValidity`. // Can be removed with next major. const isValid = this.checkValidity(this._inputValue); - this.invalid = !isValid; + this._setInvalid(!isValid); this.dispatchEvent(new CustomEvent('validated', {detail: {valid: isValid}})); return isValid; } diff --git a/test/.eslintrc.json b/test/.eslintrc.json index 86e6cee1..1e1c5ec1 100644 --- a/test/.eslintrc.json +++ b/test/.eslintrc.json @@ -1,4 +1,7 @@ { + "parserOptions": { + "ecmaVersion": 8 + }, "rules": { "no-undef": 0, "no-unused-vars": 0 diff --git a/test/helpers.html b/test/helpers.html new file mode 100644 index 00000000..a65a4e8a --- /dev/null +++ b/test/helpers.html @@ -0,0 +1,14 @@ + + + + + + + + \ No newline at end of file diff --git a/test/validation.html b/test/validation.html index a3afbd1d..5d67974b 100644 --- a/test/validation.html +++ b/test/validation.html @@ -3,10 +3,11 @@ vaadin-date-picker validation tests - + + @@ -44,5 +45,23 @@ expect(event.detail.valid).to.be.false; }); }); + describe('invalid cannot be set to false', () => { + let datePicker; + + beforeEach(async() => { + datePicker = fixture('date-picker'); + datePicker._shouldSetInvalid = (invalid) => invalid; + await nextRender(); + }); + + it('should set invalid only when it is true', async() => { + datePicker.required = true; + datePicker.validate(); + expect(datePicker.invalid).to.be.true; + datePicker.value = '2000-02-01'; + datePicker.validate(); + expect(datePicker.invalid).to.be.true; + }); + });