Code

Opened 10 months ago

Closed 10 months ago

Last modified 10 months ago

#21120 closed Cleanup/optimization (fixed)

documentation talks about validators but separates definition and usage

Reported by: nicolas@… Owned by: benhuckvale
Component: Documentation Version: 1.6-beta-1
Severity: Normal Keywords: afraid-to-commit
Cc: EvilDMP Triage Stage: Accepted
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: yes UI/UX: no

Description

The top of the page deine a validator, the answer, but not the section about the validators.

This is at the top:

""" Validators are simple functions (or callables) that take a single argument and raise ValidationError on invalid input. Validators are run after the field’s to_python and validate methods have been called. """

This is the section about validators (middle of the page):

"""
Using validators

Django’s form (and model) fields support use of simple utility functions and classes known as validators. These can be passed to a field’s constructor, via the field’s validators argument, or defined on the Field class itself with the default_validators attribute.

Simple validators can be used to validate values inside the field, let’s have a look at Django’s SlugField:

from django.forms import CharField
from django.core import validators

class SlugField(CharField):

default_validators = [validators.validate_slug]

As you can see, SlugField is just a CharField with a customized validator that validates that submitted text obeys to some character rules. This can also be done on field definition so:

slug = forms.SlugField()

is equivalent to:

slug = forms.CharField(validators=[validators.validate_slug])

"""

I think a simple example of a validator would be a nice thing to add. Or at least provide a link to the source code of the validator showcased.

This is from the 1.6 and (at the time), dev version too:

https://docs.djangoproject.com/en/1.6/ref/forms/validation/
https://docs.djangoproject.com/en/dev/ref/forms/validation/

Attachments (0)

Change History (7)

comment:1 Changed 10 months ago by timo

  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset
  • Triage Stage changed from Unreviewed to Accepted
  • Type changed from Bug to Cleanup/optimization

Yes, I think at least linking to the validators reference guide would be helpful. If you could submit a patch I'll be happy to review and commit it.

comment:2 Changed 10 months ago by EvilDMP

  • Cc EvilDMP added
  • Keywords afraid-to-commit added

comment:3 Changed 10 months ago by benhuckvale

  • Owner changed from nobody to benhuckvale
  • Status changed from new to assigned

comment:4 Changed 10 months ago by benhuckvale

I made some changes along the lines suggested, including the link to the validators reference guide.
Pull request is https://github.com/django/django/pull/1663.

comment:5 Changed 10 months ago by Tim Graham <timograham@…>

  • Resolution set to fixed
  • Status changed from assigned to closed

In 98e0453f00958af63b50e70990903eb6a04e1933:

Fixed #21120 -- Added more explicit text on using validators and link to writing validators.

Thanks nicolas at niconomicon.net for the suggestion.

comment:6 Changed 10 months ago by Tim Graham <timograham@…>

In cdb56725d4424cc58147e2ffd4426511aa95775e:

[1.6.x] Fixed #21120 -- Added more explicit text on using validators and link to writing validators.

Thanks nicolas at niconomicon.net for the suggestion.

Backport of 98e0453f00 from master

comment:7 Changed 10 months ago by Tim Graham <timograham@…>

In a722dfda93eafd6ff02b91f7a222eaccfc444ef4:

[1.5.x] Fixed #21120 -- Added more explicit text on using validators and link to writing validators.

Thanks nicolas at niconomicon.net for the suggestion.

Backport of 98e0453f00 from master

Add Comment

Modify Ticket

Change Properties
<Author field>
Action
as closed
as The resolution will be set. Next status will be 'closed'
The resolution will be deleted. Next status will be 'new'
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.