Don't bother to validate empty fields if they're not required

Fixes Et2SelectEmail complaining that empty strings are not valid email addresses
This commit is contained in:
nathan 2022-09-21 09:05:51 -06:00
parent e8e1077ece
commit d9cc794836

View File

@ -352,6 +352,13 @@ const Et2InputWidgetMixin = <T extends Constructor<LitElement>>(superclass : T)
/** /**
* Massively simplified validate, as compared to what ValidatorMixin gives us, since ValidatorMixin extends * Massively simplified validate, as compared to what ValidatorMixin gives us, since ValidatorMixin extends
* FormControlMixin which breaks SlSelect's render() * FormControlMixin which breaks SlSelect's render()
*
* We take all validators for the widget, and if there's a value (or field is required) we check the value
* with each validator. For array values we check each element with each validator. If the value does not
* pass the validator, we collect the message and display feedback to the user.
*
* We handle validation errors from the server with ManualMessages, which always "fail".
* If the value is empty, we only validate if the field is required.
*/ */
async validate() async validate()
{ {
@ -360,6 +367,8 @@ const Et2InputWidgetMixin = <T extends Constructor<LitElement>>(superclass : T)
let feedbackData = []; let feedbackData = [];
let resultPromises = []; let resultPromises = [];
this.querySelector("lion-validation-feedback")?.remove(); this.querySelector("lion-validation-feedback")?.remove();
// Collect message of a (failing) validator
const doValidate = async function(validator, value) const doValidate = async function(validator, value)
{ {
if(validator.config.fieldName) if(validator.config.fieldName)
@ -376,6 +385,8 @@ const Et2InputWidgetMixin = <T extends Constructor<LitElement>>(superclass : T)
feedbackData.push({message, type: validator.type, validator}); feedbackData.push({message, type: validator.type, validator});
}); });
}.bind(this); }.bind(this);
// Check if a validator fails
const doCheck = async(value, validator) => const doCheck = async(value, validator) =>
{ {
const result = validator.execute(value, validator.param, {node: this}); const result = validator.execute(value, validator.param, {node: this});
@ -407,7 +418,9 @@ const Et2InputWidgetMixin = <T extends Constructor<LitElement>>(superclass : T)
{ {
doCheck(values, validator); doCheck(values, validator);
} }
else // Only validate if field is required, or not required and has a value
// Don't bother to validate empty fields
else if(this.required || !this.required && this.getValue() != '')
{ {
// Validate each individual item // Validate each individual item
values.forEach((value) => doCheck(value, validator)); values.forEach((value) => doCheck(value, validator));
@ -415,6 +428,7 @@ const Et2InputWidgetMixin = <T extends Constructor<LitElement>>(superclass : T)
}); });
await Promise.all(resultPromises); await Promise.all(resultPromises);
// Show feedback from all failing validators
if(feedbackData.length > 0) if(feedbackData.length > 0)
{ {
let feedback = <LionValidationFeedback>document.createElement("lion-validation-feedback"); let feedback = <LionValidationFeedback>document.createElement("lion-validation-feedback");