Django

Code

Changeset 5218

Show
Ignore:
Timestamp:
05/13/07 21:57:42 (2 years ago)
Author:
adrian
Message:

Added unit tests and docs for the newforms case in which the form's data doesn't include a value for a nonrequired field

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • django/trunk/docs/newforms.txt

    r5217 r5218  
    187187    False 
    188188 
    189 Access the ``Form`` attribute ``errors`` to get a dictionary of error messages:: 
     189Access the ``errors`` attribute to get a dictionary of error messages:: 
    190190 
    191191    >>> f.errors 
     
    199199form's data will be validated the first time either you call ``is_valid()`` or 
    200200access ``errors``. 
     201 
     202The validation routines will only get called once, regardless of how many times 
     203you access ``errors`` or call ``is_valid()``. This means that if validation has 
     204side effects, those side effects will only be triggered once. 
    201205 
    202206Behavior of unbound forms 
     
    276280    >>> f.clean_data # Doesn't contain extra_field_1, etc. 
    277281    {'cc_myself': True, 'message': u'Hi there', 'sender': u'foo@example.com', 'subject': u'hello'} 
     282 
     283``clean_data`` will include a key and value for *all* fields defined in the 
     284``Form``, even if the data didn't include a value for fields that are not 
     285required. In this example, the data dictionary doesn't include a value for the 
     286``nick_name`` field, but ``clean_data`` includes it, with an empty value:: 
     287 
     288    >>> class OptionalPersonForm(Form): 
     289    ...     first_name = CharField() 
     290    ...     last_name = CharField() 
     291    ...     nick_name = CharField(required=False) 
     292    >>> data = {'first_name': u'John', 'last_name': u'Lennon'} 
     293    >>> f = OptionalPersonForm(data) 
     294    >>> f.is_valid() 
     295    True 
     296    >>> f.clean_data 
     297    {'nick_name': u'', 'first_name': u'John', 'last_name': u'Lennon'} 
     298 
     299In this above example, the ``clean_data`` value for ``nick_name`` is set to an 
     300empty string, because ``nick_name`` is ``CharField``, and ``CharField``s treat 
     301empty values as an empty string. Each field type knows what its "blank" value 
     302is -- e.g., for ``DateField``, it's ``None`` instead of the empty string. 
    278303 
    279304Behavior of unbound forms 
  • django/trunk/tests/regressiontests/forms/tests.py

    r5209 r5218  
    19161916>>> p.clean_data 
    19171917{'first_name': u'John', 'last_name': u'Lennon', 'birthday': datetime.date(1940, 10, 9)} 
     1918 
     1919clean_data will include a key and value for *all* fields defined in the Form, 
     1920even if the Form's data didn't include a value for fields that are not 
     1921required. In this example, the data dictionary doesn't include a value for the 
     1922"nick_name" field, but clean_data includes it. For CharFields, it's set to the 
     1923empty string. 
     1924>>> class OptionalPersonForm(Form): 
     1925...     first_name = CharField() 
     1926...     last_name = CharField() 
     1927...     nick_name = CharField(required=False) 
     1928>>> data = {'first_name': u'John', 'last_name': u'Lennon'} 
     1929>>> f = OptionalPersonForm(data) 
     1930>>> f.is_valid() 
     1931True 
     1932>>> f.clean_data 
     1933{'nick_name': u'', 'first_name': u'John', 'last_name': u'Lennon'} 
     1934 
     1935For DateFields, it's set to None. 
     1936>>> class OptionalPersonForm(Form): 
     1937...     first_name = CharField() 
     1938...     last_name = CharField() 
     1939...     birth_date = DateField(required=False) 
     1940>>> data = {'first_name': u'John', 'last_name': u'Lennon'} 
     1941>>> f = OptionalPersonForm(data) 
     1942>>> f.is_valid() 
     1943True 
     1944>>> f.clean_data 
     1945{'birth_date': None, 'first_name': u'John', 'last_name': u'Lennon'} 
    19181946 
    19191947"auto_id" tells the Form to add an "id" attribute to each form element. 
     
    33793407 
    33803408# MultiWidget and MultiValueField ############################################# 
    3381 # MultiWidgets are widgets composed of other widgets. They are usually  
     3409# MultiWidgets are widgets composed of other widgets. They are usually 
    33823410# combined with MultiValueFields - a field that is composed of other fields. 
    33833411# MulitWidgets can themselved be composed of other MultiWidgets. 
     
    33873415...     def __init__(self, attrs=None): 
    33883416...         widgets = ( 
    3389 ...             TextInput(),  
     3417...             TextInput(), 
    33903418...             SelectMultiple(choices=(('J', 'John'), ('P', 'Paul'), ('G', 'George'), ('R', 'Ringo'))), 
    33913419...             SplitDateTimeWidget(), 
     
    34123440 
    34133441>>> class ComplexField(MultiValueField): 
    3414 ...     def __init__(self, required=True, widget=None, label=None, initial=None):  
     3442...     def __init__(self, required=True, widget=None, label=None, initial=None): 
    34153443...         fields = ( 
    3416 ...             CharField(),  
     3444...             CharField(), 
    34173445...             MultipleChoiceField(choices=(('J', 'John'), ('P', 'Paul'), ('G', 'George'), ('R', 'Ringo'))), 
    34183446...             SplitDateTimeField() 
    34193447...         ) 
    3420 ...         super(ComplexField, self).__init__(fields, required, widget, label, initial)  
     3448...         super(ComplexField, self).__init__(fields, required, widget, label, initial) 
    34213449... 
    34223450...     def compress(self, data_list):