summaryrefslogtreecommitdiff
path: root/docs/ref/forms/validation.txt
diff options
context:
space:
mode:
authordjango-bot <ops@djangoproject.com>2023-03-01 13:35:43 +0100
committerMariusz Felisiak <felisiak.mariusz@gmail.com>2023-03-01 13:39:03 +0100
commit62510f01e76ad0526c94ea6d1bc6399c1ddf3df4 (patch)
tree79844be246eba809a4ca09c6f4c3448f2276321a /docs/ref/forms/validation.txt
parent32f224e359c68e70e3f9a230be0265dcd6677079 (diff)
[4.2.x] Fixed #34140 -- Reformatted code blocks in docs with blacken-docs.
Diffstat (limited to 'docs/ref/forms/validation.txt')
-rw-r--r--docs/ref/forms/validation.txt66
1 files changed, 37 insertions, 29 deletions
diff --git a/docs/ref/forms/validation.txt b/docs/ref/forms/validation.txt
index c3fa968bdb..a2b3fb4885 100644
--- a/docs/ref/forms/validation.txt
+++ b/docs/ref/forms/validation.txt
@@ -120,22 +120,22 @@ following guidelines:
* Provide a descriptive error ``code`` to the constructor::
# Good
- ValidationError(_('Invalid value'), code='invalid')
+ ValidationError(_("Invalid value"), code="invalid")
# Bad
- ValidationError(_('Invalid value'))
+ ValidationError(_("Invalid value"))
* Don't coerce variables into the message; use placeholders and the ``params``
argument of the constructor::
# Good
ValidationError(
- _('Invalid value: %(value)s'),
- params={'value': '42'},
+ _("Invalid value: %(value)s"),
+ params={"value": "42"},
)
# Bad
- ValidationError(_('Invalid value: %s') % value)
+ ValidationError(_("Invalid value: %s") % value)
* Use mapping keys instead of positional formatting. This enables putting
the variables in any order or omitting them altogether when rewriting the
@@ -143,30 +143,30 @@ following guidelines:
# Good
ValidationError(
- _('Invalid value: %(value)s'),
- params={'value': '42'},
+ _("Invalid value: %(value)s"),
+ params={"value": "42"},
)
# Bad
ValidationError(
- _('Invalid value: %s'),
- params=('42',),
+ _("Invalid value: %s"),
+ params=("42",),
)
* Wrap the message with ``gettext`` to enable translation::
# Good
- ValidationError(_('Invalid value'))
+ ValidationError(_("Invalid value"))
# Bad
- ValidationError('Invalid value')
+ ValidationError("Invalid value")
Putting it all together::
raise ValidationError(
- _('Invalid value: %(value)s'),
- code='invalid',
- params={'value': '42'},
+ _("Invalid value: %(value)s"),
+ code="invalid",
+ params={"value": "42"},
)
Following these guidelines is particularly necessary if you write reusable
@@ -176,7 +176,7 @@ While not recommended, if you are at the end of the validation chain
(i.e. your form ``clean()`` method) and you know you will *never* need
to override your error message you can still opt for the less verbose::
- ValidationError(_('Invalid value: %s') % value)
+ ValidationError(_("Invalid value: %s") % value)
The :meth:`Form.errors.as_data() <django.forms.Form.errors.as_data()>` and
:meth:`Form.errors.as_json() <django.forms.Form.errors.as_json()>` methods
@@ -194,16 +194,20 @@ As above, it is recommended to pass a list of ``ValidationError`` instances
with ``code``\s and ``params`` but a list of strings will also work::
# Good
- raise ValidationError([
- ValidationError(_('Error 1'), code='error1'),
- ValidationError(_('Error 2'), code='error2'),
- ])
+ raise ValidationError(
+ [
+ ValidationError(_("Error 1"), code="error1"),
+ ValidationError(_("Error 2"), code="error2"),
+ ]
+ )
# Bad
- raise ValidationError([
- _('Error 1'),
- _('Error 2'),
- ])
+ raise ValidationError(
+ [
+ _("Error 1"),
+ _("Error 2"),
+ ]
+ )
Using validation in practice
============================
@@ -232,6 +236,7 @@ at Django's ``SlugField``::
from django.core import validators
from django.forms import CharField
+
class SlugField(CharField):
default_validators = [validators.validate_slug]
@@ -262,13 +267,14 @@ containing comma-separated email addresses. The full class looks like this::
from django import forms
from django.core.validators import validate_email
+
class MultiEmailField(forms.Field):
def to_python(self, value):
"""Normalize data to a list of strings."""
# Return an empty list if no input was given.
if not value:
return []
- return value.split(',')
+ return value.split(",")
def validate(self, value):
"""Check if value consists only of valid emails."""
@@ -307,12 +313,13 @@ write a cleaning method that operates on the ``recipients`` field, like so::
from django import forms
from django.core.exceptions import ValidationError
+
class ContactForm(forms.Form):
# Everything as before.
...
def clean_recipients(self):
- data = self.cleaned_data['recipients']
+ data = self.cleaned_data["recipients"]
if "fred@example.com" not in data:
raise ValidationError("You have forgotten about Fred!")
@@ -349,6 +356,7 @@ example::
from django import forms
from django.core.exceptions import ValidationError
+
class ContactForm(forms.Form):
# Everything as before.
...
@@ -362,8 +370,7 @@ example::
# Only do something if both fields are valid so far.
if "help" not in subject:
raise ValidationError(
- "Did not send for 'help' in the subject despite "
- "CC'ing yourself."
+ "Did not send for 'help' in the subject despite " "CC'ing yourself."
)
In this code, if the validation error is raised, the form will display an
@@ -392,6 +399,7 @@ work out what works effectively in your particular situation. Our new code
from django import forms
+
class ContactForm(forms.Form):
# Everything as before.
...
@@ -403,8 +411,8 @@ work out what works effectively in your particular situation. Our new code
if cc_myself and subject and "help" not in subject:
msg = "Must put 'help' in subject when cc'ing yourself."
- self.add_error('cc_myself', msg)
- self.add_error('subject', msg)
+ self.add_error("cc_myself", msg)
+ self.add_error("subject", msg)
The second argument of ``add_error()`` can be a string, or preferably an
instance of ``ValidationError``. See :ref:`raising-validation-error` for more