Et2Select: Fix manual message was repeated for each selected value. Once is enough.

This commit is contained in:
nathan 2022-07-14 11:32:24 -06:00
parent 6eed7b5a0e
commit 523c785049

View File

@ -18,6 +18,7 @@ import {Et2WithSearchMixin} from "./SearchMixin";
import {Et2Tag} from "./Tag/Et2Tag";
import {LionValidationFeedback} from "@lion/form-core";
import {RowLimitedMixin} from "../Layout/RowLimitedMixin";
import {ManualMessage} from "../Validators/ManualMessage";
// export Et2WidgetWithSelect which is used as type in other modules
export class Et2WidgetWithSelect extends RowLimitedMixin(Et2widgetWithSelectMixin(SlSelect))
@ -441,6 +442,7 @@ export class Et2Select extends Et2WithSearchMixin(Et2WidgetWithSelect)
let validators = [...(this.validators || []), ...(this.defaultValidators || [])];
let fieldName = this.id;
let feedbackData = [];
let resultPromises = [];
this.querySelector("lion-validation-feedback")?.remove();
const doValidate = async function(validator, value)
{
@ -458,19 +460,7 @@ export class Et2Select extends Et2WithSearchMixin(Et2WidgetWithSelect)
feedbackData.push({message, type: validator.type, validator});
});
}.bind(this);
let resultPromises = [];
validators.map(async validator =>
{
let values = this.value;
if(!Array.isArray(values))
{
values = [values];
}
if(!values.length)
{
values = [''];
} // so required validation works
values.forEach(async value =>
const doCheck = async(value, validator) =>
{
const result = validator.execute(value, validator.param, {node: this});
if(result === true)
@ -482,7 +472,30 @@ export class Et2Select extends Et2WithSearchMixin(Et2WidgetWithSelect)
result.then(doValidate(validator, value));
resultPromises.push(result);
}
});
};
validators.map(async validator =>
{
let values = this.value;
if(!Array.isArray(values))
{
values = [values];
}
if(!values.length)
{
values = [''];
} // so required validation works
// Run manual validation messages just once, doesn't usually matter what the value is
if(validator instanceof ManualMessage)
{
doCheck(values, validator);
}
else
{
// Validate each individual item
values.forEach((value) => doCheck(value, validator));
}
});
await Promise.all(resultPromises);