Skip to content

Commit 9ba6e02

Browse files
committed
fix(validate): reset validation state when modelValue becomes undefined
1 parent 0b1cb9f commit 9ba6e02

File tree

2 files changed

+43
-1
lines changed

2 files changed

+43
-1
lines changed

packages/validate/src/ValidateMixin.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -385,7 +385,13 @@ export const ValidateMixin = dedupeMixin(
385385
* Other transitions (from Warning/Info) are not followed by a success message
386386
*/
387387
validate() {
388-
if (this.modelValue === undefined) return;
388+
if (this.modelValue === undefined) {
389+
this.constructor.validationTypes.forEach(type => {
390+
this[`${type}State`] = false;
391+
this[type] = {};
392+
});
393+
return;
394+
}
389395
this.__oldValidationStates = this.getValidationStates();
390396
this.constructor.validationTypes.forEach(type => {
391397
this.validateType(type);

packages/validate/test/ValidateMixin.test.js

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -501,6 +501,36 @@ describe('ValidateMixin', () => {
501501
expect(cbInfo.callCount).to.equal(2);
502502
expect(cbSuccess.callCount).to.equal(1);
503503
});
504+
505+
it('removes invalid states whenever modelValue becomes undefined', async () => {
506+
const el = await fixture(html`
507+
<${tag}
508+
.errorValidators=${[[minLength, { min: 3 }]]}
509+
.warningValidators=${[[minLength, { min: 5 }]]}
510+
.infoValidators=${[[minLength, { min: 7 }]]}
511+
.successValidators=${[[alwaysFalse]]}
512+
>${lightDom}</${tag}>
513+
`);
514+
515+
el.modelValue = 'a';
516+
expect(el.warningState).to.equal(true);
517+
expect(el.infoState).to.equal(true);
518+
expect(el.successState).to.equal(true);
519+
expect(el.error).to.not.eql({});
520+
expect(el.warning).to.not.eql({});
521+
expect(el.info).to.not.eql({});
522+
expect(el.success).to.not.eql({});
523+
524+
el.modelValue = undefined;
525+
expect(el.errorState).to.equal(false);
526+
expect(el.warningState).to.equal(false);
527+
expect(el.infoState).to.equal(false);
528+
expect(el.successState).to.equal(false);
529+
expect(el.error).to.eql({});
530+
expect(el.warning).to.eql({});
531+
expect(el.info).to.eql({});
532+
expect(el.success).to.eql({});
533+
});
504534
});
505535

506536
describe(`Accessibility ${suffixName}`, () => {
@@ -690,6 +720,9 @@ describe('ValidateMixin', () => {
690720
const errorRenderer = defineCE(
691721
class extends HTMLElement {
692722
renderFeedback(validationStates, message) {
723+
if (!message.list.length) {
724+
return;
725+
}
693726
const validator = message.list[0].data.validatorName;
694727
const showError = validationStates.error;
695728
this.innerText = showError ? `ERROR on ${validator}` : '';
@@ -982,6 +1015,9 @@ describe('ValidateMixin', () => {
9821015
const errorRenderer = defineCE(
9831016
class extends HTMLElement {
9841017
renderFeedback(validationStates, message) {
1018+
if (!message.list.length) {
1019+
return;
1020+
}
9851021
const validator = message.list[0].data.validatorName;
9861022
const hide =
9871023
message.list[0].data.validatorConfig &&

0 commit comments

Comments
 (0)