MCPcopy
hub / github.com/django/django / add_error

Method add_error

django/forms/forms.py:264–316  ·  view source on GitHub ↗

Update the content of `self._errors`. The `field` argument is the name of the field to which the errors should be added. If it's None, treat the errors as NON_FIELD_ERRORS. The `error` argument can be a single error, a list of errors, or a dictionary that m

(self, field, error)

Source from the content-addressed store, hash-verified

262 )
263
264 def add_error(self, field, error):
265 """
266 Update the content of `self._errors`.
267
268 The `field` argument is the name of the field to which the errors
269 should be added. If it's None, treat the errors as NON_FIELD_ERRORS.
270
271 The `error` argument can be a single error, a list of errors, or a
272 dictionary that maps field names to lists of errors. An "error" can be
273 either a simple string or an instance of ValidationError with its
274 message attribute set and a "list or dictionary" can be an actual
275 `list` or `dict` or an instance of ValidationError with its
276 `error_list` or `error_dict` attribute set.
277
278 If `error` is a dictionary, the `field` argument *must* be None and
279 errors will be added to the fields that correspond to the keys of the
280 dictionary.
281 """
282 if not isinstance(error, ValidationError):
283 # Normalize to ValidationError and let its constructor
284 # do the hard work of making sense of the input.
285 error = ValidationError(error)
286
287 if hasattr(error, "error_dict"):
288 if field is not None:
289 raise TypeError(
290 "The argument `field` must be `None` when the `error` "
291 "argument is a dictionary."
292 )
293 else:
294 error = error.error_dict
295 else:
296 error = {field or NON_FIELD_ERRORS: error.error_list}
297
298 for field, error_list in error.items():
299 if field not in self.errors:
300 if field != NON_FIELD_ERRORS and field not in self.fields:
301 raise ValueError(
302 "'%s' has no field named '%s'."
303 % (self.__class__.__name__, field)
304 )
305 if field == NON_FIELD_ERRORS:
306 self._errors[field] = self.error_class(
307 error_class="nonfield", renderer=self.renderer
308 )
309 else:
310 self._errors[field] = self.error_class(
311 renderer=self.renderer,
312 field_id=self[field].auto_id,
313 )
314 self._errors[field].extend(error_list)
315 if field in self.cleaned_data:
316 del self.cleaned_data[field]
317
318 def has_error(self, field, code=None):
319 return field in self.errors and (

Callers 13

_clean_fieldsMethod · 0.95
_clean_formMethod · 0.95
validate_passwordsMethod · 0.80
validate_passwordsMethod · 0.80
_update_errorsMethod · 0.80
cleanMethod · 0.80
cleanMethod · 0.80
form_validMethod · 0.80

Calls 3

ValidationErrorClass · 0.90
extendMethod · 0.80
itemsMethod · 0.45

Tested by 5

cleanMethod · 0.64
cleanMethod · 0.64