MCPcopy
hub / github.com/django/django / clean

Method clean

tests/forms_tests/tests/test_forms.py:1571–1597  ·  view source on GitHub ↗
(self)

Source from the content-addressed store, hash-verified

1569 password2 = CharField(widget=PasswordInput)
1570
1571 def clean(self):
1572 # Test raising a ValidationError as NON_FIELD_ERRORS.
1573 if (
1574 self.cleaned_data.get("password1")
1575 and self.cleaned_data.get("password2")
1576 and self.cleaned_data["password1"] != self.cleaned_data["password2"]
1577 ):
1578 raise ValidationError("Please make sure your passwords match.")
1579
1580 # Test raising ValidationError that targets multiple fields.
1581 errors = {}
1582 if self.cleaned_data.get("password1") == "FORBIDDEN_VALUE":
1583 errors["password1"] = "Forbidden value."
1584 if self.cleaned_data.get("password2") == "FORBIDDEN_VALUE":
1585 errors["password2"] = ["Forbidden value."]
1586 if errors:
1587 raise ValidationError(errors)
1588
1589 # Test Form.add_error()
1590 if self.cleaned_data.get("password1") == "FORBIDDEN_VALUE2":
1591 self.add_error(None, "Non-field error 1.")
1592 self.add_error("password1", "Forbidden value 2.")
1593 if self.cleaned_data.get("password2") == "FORBIDDEN_VALUE2":
1594 self.add_error("password2", "Forbidden value 2.")
1595 raise ValidationError("Non-field error 2.")
1596
1597 return self.cleaned_data
1598
1599 f = UserRegistration(auto_id=False)
1600 self.assertEqual(f.errors, {})

Callers

nothing calls this directly

Calls 3

ValidationErrorClass · 0.85
add_errorMethod · 0.80
getMethod · 0.45

Tested by

no test coverage detected