Django

Code

Changeset 2777

Show
Ignore:
Timestamp:
04/28/06 20:13:30 (2 years ago)
Author:
adrian
Message:

magic-removal: Fixed #1703 -- Updated forms and manipulator docs. Thanks, Malcolm

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • django/branches/magic-removal/docs/forms.txt

    r2770 r2777  
    5454    >>> from mysite.myapp.models import Place 
    5555    >>> Place.AddManipulator 
    56     <class Place.ManipulatorAdd at 0x4c1540
     56    <class 'django.models.manipulators.AddManipulator'
    5757    >>> Place.ChangeManipulator 
    58     <class Place.ManipulatorChange at 0x4c1630
     58    <class 'django.models.manipulators.ChangeManipulator'
    5959 
    6060Using the ``AddManipulator`` 
     
    6666    from django.shortcuts import render_to_response 
    6767    from django.http import Http404, HttpResponse, HttpResponseRedirect 
     68    from django import forms 
    6869    from mysite.myapp.models import Place 
    69     from django import forms 
    7070 
    7171    def naive_create_place(request): 
     
    199199context. 
    200200 
    201 .. admonition:: Philosophy:: 
     201.. admonition:: Philosophy: 
    202202 
    203203    Finally, for the HTTP purists in the audience (and the authorship), this 
     
    409409done using a simple validation API: A validator is a callable that raises a 
    410410``ValidationError`` if there's something wrong with the data. 
    411 ``django.core.validators`` defines a host of validator functions, but defining 
    412 your own couldn't be easier:: 
     411``django.core.validators`` defines a host of validator functions (see below), 
     412but defining your own couldn't be easier:: 
    413413 
    414414    from django.core import validators 
     
    432432The arguments to a validator function take a little explanation.  ``field_data`` 
    433433is the value of the field in question, and ``all_data`` is a dictionary of all 
    434 the data being validated.  Note that at the point validators are called all 
    435 data will still be strings (as ``do_html2python`` hasn't been called yet). 
     434the data being validated. 
     435 
     436.. admonition:: Note:: 
     437 
     438    At the point validators are called all data will still be 
     439    strings (as ``do_html2python`` hasn't been called yet). 
    436440 
    437441Also, because consistency in user interfaces is important, we strongly urge you 
    438442to put punctuation at the end of your validation messages. 
    439443 
     444Ready-made Validators 
     445--------------------- 
     446 
     447Writing your own validator is not difficult, but there are some situations 
     448that come up over and over again. Django comes with a number of validators 
     449that can be used directly in your code. All of these functions and classes 
     450reside in ``django/core/validators.py``. 
     451 
     452The following validators should all be self-explanatory. Each one provides a 
     453check for the given property: 
     454 
     455    * isAlphaNumeric 
     456    * isAlphaNumericURL 
     457    * isSlug 
     458    * isLowerCase 
     459    * isUpperCase 
     460    * isCommaSeparatedIntegerList 
     461    * isCommaSeparatedEmailList 
     462    * isValidIPAddress4 
     463    * isNotEmpty 
     464    * isOnlyDigits 
     465    * isNotOnlyDigits 
     466    * isInteger 
     467    * isOnlyLetters 
     468    * isValidANSIDate 
     469    * isValidANSITime 
     470    * isValidEmail 
     471    * isValidImage 
     472    * isValidImageURL 
     473    * isValidPhone 
     474    * isValidQuicktimeVideoURL 
     475    * isValidURL 
     476    * isValidHTML 
     477    * isWellFormedXml 
     478    * isWellFormedXmlFragment 
     479    * isExistingURL 
     480    * isValidUSState 
     481    * hasNoProfanities 
     482 
     483There are also a group of validators that are slightly more flexible. For 
     484these validators, you create a validator instance, passing in the parameters 
     485described below. The returned object is a callable that can be used as a 
     486validator. 
     487 
     488For example:: 
     489 
     490    from django.core import validators 
     491    from django import forms 
     492 
     493    power_validator = validators.IsAPowerOf(2) 
     494 
     495    class InstallationManipulator(forms.Manipulator) 
     496        def __init__(self): 
     497            self.fields = ( 
     498                ... 
     499                forms.IntegerField(field_name = "size", validator_list=[power_validator]) 
     500            ) 
     501 
     502Here, ``validators.IsAPowerOf(...)`` returned something that could be used as 
     503a validator (in this case, a check that a number was a power of 2). 
     504 
     505Each of the standard validators that take parameters have an optional final 
     506argument (``error_message``) that is the message returned when validation 
     507fails. If no message is passed in, a default message is used. 
     508 
     509``AlwaysMatchesOtherField`` 
     510    Takes a field name and the current field is valid if and only if its value 
     511    matches the contents of the other field. 
     512 
     513``ValidateIfOtherFieldEquals`` 
     514    Takes three parameters: ``other_field``, ``other_value`` and 
     515    ``validator_list``, in that order. If ``other_field`` has a value of 
     516    ``other_vaue``, then the validators in ``validator_list`` are all run 
     517    against the current field. 
     518 
     519``RequiredIfOtherFieldNotGiven`` 
     520    Takes the name of the other field and this field is only required if the 
     521    other field has no value. 
     522 
     523``RequiredIfOtherFieldsNotGiven`` 
     524    Similar to ``RequiredIfOtherFieldNotGiven``, except that it takes a list 
     525    of field names and if any one of the supplied fields does not have a value 
     526    provided, the field being validated is required. 
     527 
     528``RequiredIfOtherFieldEquals`` and ``RequiredIfOtherFieldDoesNotEqual`` 
     529    Each of these validator classes takes a field name and a value (in that 
     530    order). If the given field does (or does not have, in the latter case) the 
     531    given value, then the current field being validated is required. 
     532 
     533    Note that because validators are called before any ``do_html2python()`` 
     534    functions, the value being compared against is a string. So 
     535    ``RequiredIfOtherFieldEquals('choice', '1')`` is correct, whilst 
     536    ``RequiredIfOtherFieldEquals('choice', 1)`` will never result in the 
     537    equality test succeeding. 
     538 
     539``IsLessThanOtherField`` 
     540    Takes a field name and validates that the current field being validated 
     541    has a value that is less than (or equal to) the other field's value. 
     542    Again, comparisons are done using strings, so be cautious about using 
     543    this function to compare data that should be treated as another type. The 
     544    string "123" is less than the string "2", for example. If you don't want 
     545    string comparison here, you will need to write your own validator. 
     546 
     547``IsAPowerOf`` 
     548    Takes an integer argument and when called as a validator, checks that the 
     549    field being validated is a power of the integer. 
     550 
     551``IsValidFloat`` 
     552    Takes a maximum number of digits and number of decimal places (in that 
     553    order) and validates whether the field is a float with less than the 
     554    maximum number of digits and decimal place. 
     555 
     556``MatchesRegularExpression`` 
     557    Takes a regular expression (a string) as a parameter and validates the 
     558    field value against it. 
     559 
     560``AnyValidator`` 
     561    Takes a list of validators as a parameter. At validation time, if the 
     562    field successfully validates against any one of the validators, it passes 
     563    validation. The validators are tested in the order specified in the 
     564    original list. 
     565 
     566``URLMimeTypeCheck`` 
     567    Used to validate URL fields. Takes a list of MIME types (such as 
     568    ``text/plain``) at creation time. At validation time, it verifies that the 
     569    field is indeed a URL and then tries to retrieve the content at the URL. 
     570    Validation succeeds if the content could be retrieved and it has a content 
     571    type from the list used to create the validator. 
     572 
     573``RelaxNGCompact`` 
     574    Used to validate an XML document against a Relax NG compact schema. Takes 
     575    a file path to the location of the schema and an optional root element 
     576    (which is wrapped around the XML fragment before validation, if supplied). 
     577    At validation time, the XML fragment is validated against the schema using 
     578    the executable specified in the ``JING_PATH`` setting (see the settings_ 
     579    document for more details). 
     580 
    440581.. _`generic views`: http://www.djangoproject.com/documentation/generic_views/ 
     582.. _`models API`: http://www.djangoproject.com/documentation/model_api/ 
     583.. _settings: http://www.djangoproject.com/documentation/settings/