Feat polifill for getFirstError and make existsError accept more than one field (kcContext.messagePerField)

This commit is contained in:
Joseph Garrone 2024-04-13 04:28:28 +02:00
parent 00651c0c3c
commit a0367066b4
2 changed files with 59 additions and 29 deletions

View File

@ -79,7 +79,9 @@
</#if>
},
"existsError": function (fieldName) {
"existsError": function (){
function existsError_singleFieldName(fieldName) {
<#if !messagesPerField?? || !(messagesPerField?is_hash)>
throw new Error("You're not supposed to use messagesPerField.printIfExists in this page");
@ -121,6 +123,18 @@
</#if>
}
for( let i = 0; i < arguments.length; i++ ){
if( existsError_singleFieldName(arguments[i]) ){
return true;
}
}
return false;
},
"get": function (fieldName) {
@ -219,6 +233,19 @@
throw new Error(fieldName + "is probably runtime generated, see: https://docs.keycloakify.dev/limitations#field-names-cant-be-runtime-generated");
</#if>
},
"getFirstError": function () {
for( let i = 0; i < arguments.length; i++ ){
const fieldName = arguments[i];
if( out.messagesPerField.existsError(fieldName) ){
return out.messagesPerField.get(fieldName);
}
}
}
};

View File

@ -101,7 +101,7 @@ export declare namespace KcContext {
* @param fields
* @return boolean
*/
existsError: (fieldName: string) => boolean;
existsError: (fieldName: string, ...otherFiledNames: string[]) => boolean;
/**
* Get message for given field.
*
@ -116,6 +116,8 @@ export declare namespace KcContext {
* @return boolean
*/
exists: (fieldName: string) => boolean;
getFirstError: (...fieldNames: string[]) => string;
};
properties: Record<string, string | undefined>;
authenticationSession?: {
@ -165,6 +167,7 @@ export declare namespace KcContext {
alias: string;
providerId: string;
displayName: string;
iconClasses?: string;
}[];
};
};