@@ -9,11 +9,11 @@ interface ValidationCheckOptions {
99/**
1010 * Extracts validity errors from the element and merges with custom errors.
1111 */
12- function extractErrors ( el : FormElement , custom ?: Record < string , boolean > ) : Record < string , boolean > {
13- const output : Record < string , boolean > = { } ;
12+ function extractErrors ( el : FormElement , custom ?: Record < string , string > ) : Record < string , string > {
13+ const output : Record < string , string > = { } ;
1414 for ( const key in el . validity ) {
1515 if ( key !== 'valid' && el . validity [ key as keyof ValidityState ] ) {
16- output [ key ] = true ;
16+ output [ key ] = el . validationMessage || key ;
1717 }
1818 }
1919 return { ...output , ...custom } ;
@@ -22,23 +22,17 @@ function extractErrors(el: FormElement, custom?: Record<string, boolean>): Recor
2222/**
2323 * Gets the result of any custom validations available on the fields.
2424 */
25- function getCustomValidations (
26- value : unknown ,
27- values : Record < string , unknown > ,
28- validations : Record < string , ValidatorFn > = { }
29- ) : [ Record < string , string > , Record < string , boolean > ] {
30- const messages : Record < string , string > = { } ;
31- const errors : Record < string , boolean > = { } ;
25+ function getCustomValidations ( value : unknown , values : Record < string , unknown > , validations : Record < string , ValidatorFn > = { } ) : Record < string , string > {
26+ const errors : Record < string , string > = { } ;
3227
3328 Object . entries ( validations ) . forEach ( ( [ key , validation ] ) => {
3429 const message = validation ( value , values ) ;
3530 if ( message !== null ) {
36- messages [ key ] = message ;
37- errors [ key ] = true ;
31+ errors [ key ] = message ;
3832 }
3933 } ) ;
4034
41- return [ messages , errors ] ;
35+ return errors ;
4236}
4337
4438/**
@@ -48,7 +42,7 @@ export function createValidationChecker(
4842 inputGroup : string ,
4943 elementGroup : FormElement [ ] ,
5044 values : Record < string , unknown > ,
51- options ?: ValidationCheckOptions
45+ options ?: ValidationCheckOptions ,
5246) : ( el : FormElement , elValue : unknown ) => FieldValidity {
5347 return ( el : FormElement , elValue : unknown ) : FieldValidity => {
5448 // Reset the validity
@@ -78,26 +72,20 @@ export function createValidationChecker(
7872 } ;
7973
8074 // Check for any custom validations
81- const [ messages , customErrors ] = getCustomValidations (
82- elValue ,
83- values ,
84- options ?. validators ?. [ inputGroup ]
85- ) ;
75+ const customErrors = getCustomValidations ( elValue , values , options ?. validators ?. [ inputGroup ] ) ;
8676
8777 const errors = extractErrors ( el , customErrors ) ;
8878 const errorKeys = Object . keys ( errors ) ;
8979
9080 if ( el . checkValidity ( ) ) {
9181 if ( errorKeys . length > 0 ) {
92- el . setCustomValidity ( messages [ errorKeys [ 0 ] ] ) ;
82+ el . setCustomValidity ( errors [ errorKeys [ 0 ] ] ) ;
9383 }
9484 } else {
9585 if ( customMessages [ errorKeys [ 0 ] ] ) {
9686 el . setCustomValidity ( customMessages [ errorKeys [ 0 ] ] ) ;
9787 }
9888 }
99-
100- // Recheck validity and show any messages
10189 const valid = el . checkValidity ( ) ;
10290 if ( ! valid ) {
10391 el . setAttribute ( 'data-formula-invalid' , 'true' ) ;
0 commit comments