@@ -251,7 +251,7 @@ export type SuperForm<T extends ZodValidation<AnyZodObject>, M = any> = {
251251 } ;
252252 formId : Writable < string | undefined > ;
253253 errors : Writable < Validation < T , M > [ 'errors' ] > & {
254- clear : ( undefinePath ?: string [ ] ) => void ;
254+ clear : ( ) => void ;
255255 } ;
256256 constraints : Writable < Validation < T , M > [ 'constraints' ] > ;
257257 message : Writable < Validation < T , M > [ 'message' ] > ;
@@ -578,17 +578,11 @@ export function superForm<
578578 * To work with client-side validation, errors cannot be deleted but must
579579 * be set to undefined, to know where they existed before (tainted+error check in oninput)
580580 */
581- clear : ( undefinePath ?: string [ ] ) => {
582- _errors . update ( ( $errors ) => {
583- traversePaths ( $errors , ( pathData ) => {
584- if ( Array . isArray ( pathData . value ) ) {
585- return pathData . set ( undefined ) ;
586- }
587- } ) ;
588- if ( undefinePath ) setPaths ( $errors , [ undefinePath ] , undefined ) ;
589- return $errors ;
590- } ) ;
591- }
581+ clear : ( ) =>
582+ clearErrors ( _errors , {
583+ undefinePath : null ,
584+ clearFormLevelErrors : true
585+ } )
592586 } ;
593587
594588 const Tainted = writable < TaintedFields < T2 > | undefined > ( ) ;
@@ -948,6 +942,34 @@ function shouldSyncFlash<T extends AnyZodObject, M>(
948942 return options . syncFlashMessage ;
949943}
950944
945+ function clearErrors < T extends AnyZodObject > (
946+ Errors : Writable < ValidationErrors < T > > ,
947+ options : {
948+ undefinePath : string [ ] | null ;
949+ clearFormLevelErrors : boolean ;
950+ }
951+ ) {
952+ Errors . update ( ( $errors ) => {
953+ traversePaths ( $errors , ( pathData ) => {
954+ if (
955+ pathData . path . length == 1 &&
956+ pathData . path [ 0 ] == '_errors' &&
957+ ! options . clearFormLevelErrors
958+ ) {
959+ return ;
960+ }
961+ if ( Array . isArray ( pathData . value ) ) {
962+ return pathData . set ( undefined ) ;
963+ }
964+ } ) ;
965+
966+ if ( options . undefinePath )
967+ setPaths ( $errors , [ options . undefinePath ] , undefined ) ;
968+
969+ return $errors ;
970+ } ) ;
971+ }
972+
951973const effectMapCache = new WeakMap < object , boolean > ( ) ;
952974
953975// @DCI -context
@@ -1022,8 +1044,10 @@ async function validateField<T extends AnyZodObject, M>(
10221044 return get ( Errors ) ;
10231045 }
10241046
1025- function Errors_clear ( undefinePath : string [ ] ) {
1026- Errors . clear ( undefinePath ) ;
1047+ function Errors_clear (
1048+ options : NonNullable < Parameters < typeof clearErrors > [ 1 ] >
1049+ ) {
1050+ return clearErrors ( Errors , options ) ;
10271051 }
10281052
10291053 function Errors_set ( newErrors : ValidationErrors < UnwrapEffects < T > > ) {
@@ -1195,7 +1219,7 @@ async function validateField<T extends AnyZodObject, M>(
11951219 // We validated the whole data structure, so clear all errors on success
11961220 // but also set the current path to undefined, so it will be used in the tainted+error
11971221 // check in oninput.
1198- Errors_clear ( path ) ;
1222+ Errors_clear ( { undefinePath : path , clearFormLevelErrors : false } ) ;
11991223 return undefined ;
12001224 }
12011225 } else {
0 commit comments