11import copy
2- from typing import Union , List , Tuple
2+ from typing import List , Tuple
33
44from django .core .exceptions import ValidationError
55from django .forms import fields_for_model
@@ -95,18 +95,25 @@ def errors(self) -> dict:
9595 def is_valid (self ) -> bool :
9696 return not self .errors
9797
98- def add_error (self , field : Union [ str , Tuple ] , errors : ValidationError ):
98+ def add_error (self , field : Tuple , errors : ValidationError ):
9999 if hasattr (errors , 'error_dict' ):
100- for item in errors .error_dict .values ():
101- for error in item :
100+ for key , items in errors .error_dict .items ():
101+ for error in items :
102102 if isinstance (error , DetailValidationError ):
103103 error .prepend (field )
104104 self .add_error (error .path , error )
105+ elif isinstance (error , ValidationError ):
106+ self .add_error (field + (key , ), error )
105107 elif not hasattr (errors , 'message' ) and isinstance (errors .error_list , list ):
106108 for item in errors .error_list :
107109 if isinstance (item , DetailValidationError ):
108110 item .prepend (field )
109111 self .add_error (item .path , item )
112+ elif isinstance (item , ValidationError ):
113+ path = field
114+ if hasattr (item , 'path' ):
115+ path = field + item .path
116+ self .add_error (path , item )
110117 else :
111118 self ._errors .append (
112119 DetailValidationError (errors , (field ,) if isinstance (field , str ) else field )
@@ -132,13 +139,13 @@ def full_clean(self):
132139 if hasattr (self , f"clean_{ key } " ):
133140 self .cleaned_data [key ] = getattr (self , f"clean_{ key } " )()
134141 except ValidationError as e :
135- self .add_error (key , e )
142+ self .add_error (( key , ) , e )
136143 except (AttributeError , TypeError , ValueError ):
137- self .add_error (key , ValidationError (_ ("Invalid value" )))
144+ self .add_error (( key , ) , ValidationError (_ ("Invalid value" )))
138145 try :
139146 cleaned_data = self .clean ()
140147 except ValidationError as e :
141- self .add_error ('$body' , e )
148+ self .add_error (( '$body' , ) , e )
142149 else :
143150 if cleaned_data is not None :
144151 self .cleaned_data = cleaned_data
0 commit comments