# -*- coding: utf-8 -*- tests = r""" >>> from django.newforms import * >>> import datetime >>> import time >>> import re >>> try: ... from decimal import Decimal ... except ImportError: ... from django.utils._decimal import Decimal ######### # Forms # ######### A Form is a collection of Fields. It knows how to validate a set of data and it knows how to render itself in a couple of default ways (e.g., an HTML table). You can pass it data in __init__(), as a dictionary. # Form ######################################################################## >>> class Person(Form): ... first_name = CharField() ... last_name = CharField() ... birthday = DateField() Pass a dictionary to a Form's __init__(). >>> p = Person({'first_name': u'John', 'last_name': u'Lennon', 'birthday': u'1940-10-9'}) >>> p.is_bound True >>> p.errors {} >>> p.is_valid() True >>> p.errors.as_ul() u'' >>> p.errors.as_text() u'' >>> p.cleaned_data["first_name"], p.cleaned_data["last_name"], p.cleaned_data["birthday"] (u'John', u'Lennon', datetime.date(1940, 10, 9)) >>> print p['first_name'] >>> print p['last_name'] >>> print p['birthday'] >>> print p['nonexistentfield'] Traceback (most recent call last): ... KeyError: "Key 'nonexistentfield' not found in Form" >>> for boundfield in p: ... print boundfield >>> for boundfield in p: ... print boundfield.label, boundfield.data First name John Last name Lennon Birthday 1940-10-9 >>> print p Empty dictionaries are valid, too. >>> p = Person({}) >>> p.is_bound True >>> p.errors['first_name'] [u'This field is required.'] >>> p.errors['last_name'] [u'This field is required.'] >>> p.errors['birthday'] [u'This field is required.'] >>> p.is_valid() False >>> p.cleaned_data Traceback (most recent call last): ... AttributeError: 'Person' object has no attribute 'cleaned_data' >>> print p >>> print p.as_table() >>> print p.as_ul()
  • >>> print p.as_p()

    If you don't pass any values to the Form's __init__(), or if you pass None, the Form will be considered unbound and won't do any validation. Form.errors will be an empty dictionary *but* Form.is_valid() will return False. >>> p = Person() >>> p.is_bound False >>> p.errors {} >>> p.is_valid() False >>> p.cleaned_data Traceback (most recent call last): ... AttributeError: 'Person' object has no attribute 'cleaned_data' >>> print p >>> print p.as_table() >>> print p.as_ul()
  • >>> print p.as_p()

    Unicode values are handled properly. >>> p = Person({'first_name': u'John', 'last_name': u'\u0160\u0110\u0106\u017d\u0107\u017e\u0161\u0111', 'birthday': '1940-10-9'}) >>> p.as_table() u'\n\n' >>> p.as_ul() u'
  • \n
  • \n
  • ' >>> p.as_p() u'

    \n

    \n

    ' >>> p = Person({'last_name': u'Lennon'}) >>> p.errors['first_name'] [u'This field is required.'] >>> p.errors['birthday'] [u'This field is required.'] >>> p.is_valid() False >>> p.errors.as_ul() u'' >>> print p.errors.as_text() * first_name * This field is required. * birthday * This field is required. >>> p.cleaned_data Traceback (most recent call last): ... AttributeError: 'Person' object has no attribute 'cleaned_data' >>> p['first_name'].errors [u'This field is required.'] >>> p['first_name'].errors.as_ul() u'' >>> p['first_name'].errors.as_text() u'* This field is required.' >>> p = Person() >>> print p['first_name'] >>> print p['last_name'] >>> print p['birthday'] cleaned_data will always *only* contain a key for fields defined in the Form, even if you pass extra data when you define the Form. In this example, we pass a bunch of extra fields to the form constructor, but cleaned_data contains only the form's fields. >>> data = {'first_name': u'John', 'last_name': u'Lennon', 'birthday': u'1940-10-9', 'extra1': 'hello', 'extra2': 'hello'} >>> p = Person(data) >>> p.is_valid() True >>> p.cleaned_data['first_name'] u'John' >>> p.cleaned_data['last_name'] u'Lennon' >>> p.cleaned_data['birthday'] datetime.date(1940, 10, 9) cleaned_data will include a key and value for *all* fields defined in the Form, even if the Form's data didn't include a value for fields that are not required. In this example, the data dictionary doesn't include a value for the "nick_name" field, but cleaned_data includes it. For CharFields, it's set to the empty string. >>> class OptionalPersonForm(Form): ... first_name = CharField() ... last_name = CharField() ... nick_name = CharField(required=False) >>> data = {'first_name': u'John', 'last_name': u'Lennon'} >>> f = OptionalPersonForm(data) >>> f.is_valid() True >>> f.cleaned_data['nick_name'] u'' >>> f.cleaned_data['first_name'] u'John' >>> f.cleaned_data['last_name'] u'Lennon' For DateFields, it's set to None. >>> class OptionalPersonForm(Form): ... first_name = CharField() ... last_name = CharField() ... birth_date = DateField(required=False) >>> data = {'first_name': u'John', 'last_name': u'Lennon'} >>> f = OptionalPersonForm(data) >>> f.is_valid() True >>> print f.cleaned_data['birth_date'] None >>> f.cleaned_data['first_name'] u'John' >>> f.cleaned_data['last_name'] u'Lennon' "auto_id" tells the Form to add an "id" attribute to each form element. If it's a string that contains '%s', Django will use that as a format string into which the field's name will be inserted. It will also put a