rename message prop to body

This commit is contained in:
Niels Lyngsø
2024-08-13 10:17:23 +02:00
parent 47fc18c1e9
commit d763f804c8
3 changed files with 11 additions and 12 deletions

View File

@@ -8,7 +8,7 @@ export interface UmbValidationMessage {
type: UmbValidationMessageType;
key: string;
path: string;
message: string;
body: string;
}
export class UmbValidationMessagesManager {
@@ -72,24 +72,23 @@ export class UmbValidationMessagesManager {
);
}
addMessage(type: UmbValidationMessageType, path: string, message: string, key: string = UmbId.new()): void {
addMessage(type: UmbValidationMessageType, path: string, body: string, key: string = UmbId.new()): void {
path = this.#translatePath(path) ?? path;
// check if there is an existing message with the same path and type, and append the new messages: [NL]
if (this.#messages.getValue().find((x) => x.type === type && x.path === path && x.message === message)) {
if (this.#messages.getValue().find((x) => x.type === type && x.path === path && x.body === body)) {
return;
}
this.#messages.appendOne({ type, key, path, message });
this.#messages.appendOne({ type, key, path, body: body });
}
addMessages(type: UmbValidationMessageType, path: string, messages: Array<string>): void {
addMessages(type: UmbValidationMessageType, path: string, bodies: Array<string>): void {
path = this.#translatePath(path) ?? path;
// filter out existing messages with the same path and type, and append the new messages: [NL]
const existingMessages = this.#messages.getValue();
const newMessages = messages.filter(
(message) =>
existingMessages.find((x) => x.type === type && x.path === path && x.message === message) === undefined,
const newBodies = bodies.filter(
(message) => existingMessages.find((x) => x.type === type && x.path === path && x.body === message) === undefined,
);
this.#messages.append(newMessages.map((message) => ({ type, key: UmbId.new(), path, message })));
this.#messages.append(newBodies.map((body) => ({ type, key: UmbId.new(), path, body })));
}
/*

View File

@@ -109,7 +109,7 @@ export class UmbValidationContext extends UmbControllerBase implements UmbValida
// TODO: Subtract the base path from the path, so it becomes local to this context:
const path = ReplaceStartOfString(msg.path, this.#baseDataPath!, '$');
// Notice, the local message uses the same key. [NL]
this.messages.addMessage(msg.type, path, msg.message, msg.key);
this.messages.addMessage(msg.type, path, msg.body, msg.key);
});
},
'observeParentMessages',
@@ -133,7 +133,7 @@ export class UmbValidationContext extends UmbControllerBase implements UmbValida
// replace this.#baseDataPath (if it starts with it) with $ in the path, so it becomes relative to the parent context
const path = ReplaceStartOfString(msg.path, '$', this.#baseDataPath!);
// Notice, the parent message uses the same key. [NL]
this.#parent!.messages.addMessage(msg.type, path, msg.message, msg.key);
this.#parent!.messages.addMessage(msg.type, path, msg.body, msg.key);
});
},
'observeLocalMessages',

View File

@@ -66,7 +66,7 @@ export class UmbBindServerValidationToFormControl extends UmbControllerBase {
if (!this.#controlValidator) {
this.#controlValidator = this.#control.addValidator(
'customError',
() => this.#messages.map((x) => x.message).join(', '),
() => this.#messages.map((x) => x.body).join(', '),
() => !this.#isValid,
);
//this.#control.addEventListener('change', this.#onControlChange);