From 9127f08440555fd08e11cc55315aa530dc09819b Mon Sep 17 00:00:00 2001 From: Ben Amor Aymen Date: Wed, 19 Feb 2020 17:20:47 +0100 Subject: [PATCH] fix(field): do not validate disabled fields (#586) --- packages/field/src/LionField.js | 7 +-- packages/field/test/lion-field.test.js | 36 +++++++++++ .../17-Validation-Examples.stories.mdx | 61 +++++++++++++++++++ packages/validate/src/ValidateMixin.js | 6 ++ 4 files changed, 106 insertions(+), 4 deletions(-) diff --git a/packages/field/src/LionField.js b/packages/field/src/LionField.js index be80f3476..957059232 100644 --- a/packages/field/src/LionField.js +++ b/packages/field/src/LionField.js @@ -125,10 +125,9 @@ export class LionField extends FormControlMixin( updated(changedProps) { super.updated(changedProps); - if (changedProps.has('disabled') && this.disabled) { - this._inputNode.disabled = true; - } else if (changedProps.has('disabled')) { - this._inputNode.disabled = false; + if (changedProps.has('disabled')) { + this._inputNode.disabled = this.disabled; + this.validate(); } if (changedProps.has('name')) { diff --git a/packages/field/test/lion-field.test.js b/packages/field/test/lion-field.test.js index 4177e2195..99c8c8bc5 100644 --- a/packages/field/test/lion-field.test.js +++ b/packages/field/test/lion-field.test.js @@ -359,6 +359,42 @@ describe('', () => { wantedShowsFeedbackFor: [], }); }); + it('should not run validation when disabled', async () => { + const HasX = class extends Validator { + constructor() { + super(); + this.name = 'HasX'; + } + + execute(value) { + const result = value.indexOf('x') === -1; + return result; + } + }; + const disabledEl = await fixture(html` + <${tag} + disabled + .validators=${[new HasX()]} + .modelValue=${'a@b.nl'} + > + ${inputSlot} + + `); + const el = await fixture(html` + <${tag} + .validators=${[new HasX()]} + .modelValue=${'a@b.nl'} + > + ${inputSlot} + + `); + + expect(el.hasFeedbackFor).to.deep.equal(['error']); + expect(el.validationStates.error).to.have.a.property('HasX'); + + expect(disabledEl.hasFeedbackFor).to.deep.equal([]); + expect(disabledEl.validationStates.error).to.equal(undefined); + }); it('can be required', async () => { const el = await fixture(html` diff --git a/packages/form-system/stories/17-Validation-Examples.stories.mdx b/packages/form-system/stories/17-Validation-Examples.stories.mdx index b4aba46e7..3fd22e2a9 100644 --- a/packages/form-system/stories/17-Validation-Examples.stories.mdx +++ b/packages/form-system/stories/17-Validation-Examples.stories.mdx @@ -615,3 +615,64 @@ const minDateValidatorRef = new MinDate(beginDate, { .validators="${[minDateValidatorRef]}" > ``` + +## Disabled inputs validation + +According to the W3C specs, Disabled fields should not be validated. +Therefor if the attribute disabled is present on a lion-input it will not be validated. + + + {html` + + + + + `} + + +```html + + + + +``` diff --git a/packages/validate/src/ValidateMixin.js b/packages/validate/src/ValidateMixin.js index 8252ab75f..e13fa7f4d 100644 --- a/packages/validate/src/ValidateMixin.js +++ b/packages/validate/src/ValidateMixin.js @@ -261,6 +261,12 @@ export const ValidateMixin = dedupeMixin( * call. Situation B will occur after every call. */ async validate({ clearCurrentResult } = {}) { + if (this.disabled) { + this.__clearValidationResults(); + this.__validationResult = []; + this._updateFeedbackComponent(); + return; + } if (!this.__validateInitialized) { return; }