newforms: Changed Form.ignore_errors to Form.is_bound, which is more descriptive and can be helpful to access at runtime

git-svn-id: http://code.djangoproject.com/svn/django/trunk@4286 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
Adrian Holovaty 2007-01-05 00:04:38 +00:00
parent 61ede43202
commit 02f690f738
2 changed files with 12 additions and 6 deletions

View File

@ -40,7 +40,7 @@ class BaseForm(StrAndUnicode):
# information. Any improvements to the form API should be made to *this* # information. Any improvements to the form API should be made to *this*
# class, not to the Form class. # class, not to the Form class.
def __init__(self, data=None, auto_id='id_%s', prefix=None): def __init__(self, data=None, auto_id='id_%s', prefix=None):
self.ignore_errors = data is None self.is_bound = data is not None
self.data = data or {} self.data = data or {}
self.auto_id = auto_id self.auto_id = auto_id
self.prefix = prefix self.prefix = prefix
@ -73,7 +73,7 @@ class BaseForm(StrAndUnicode):
Returns True if the form has no errors. Otherwise, False. If errors are Returns True if the form has no errors. Otherwise, False. If errors are
being ignored, returns False. being ignored, returns False.
""" """
return not self.ignore_errors and not bool(self.errors) return self.is_bound and not bool(self.errors)
def add_prefix(self, field_name): def add_prefix(self, field_name):
""" """
@ -137,7 +137,7 @@ class BaseForm(StrAndUnicode):
Cleans all of self.data and populates self.__errors and self.clean_data. Cleans all of self.data and populates self.__errors and self.clean_data.
""" """
errors = ErrorDict() errors = ErrorDict()
if self.ignore_errors: # Stop further processing. if not self.is_bound: # Stop further processing.
self.__errors = errors self.__errors = errors
return return
self.clean_data = {} self.clean_data = {}
@ -217,7 +217,7 @@ class BoundField(StrAndUnicode):
auto_id = self.auto_id auto_id = self.auto_id
if auto_id and not attrs.has_key('id') and not widget.attrs.has_key('id'): if auto_id and not attrs.has_key('id') and not widget.attrs.has_key('id'):
attrs['id'] = auto_id attrs['id'] = auto_id
if self.form.ignore_errors: if not self.form.is_bound:
data = self.field.initial data = self.field.initial
else: else:
data = self.data data = self.data

View File

@ -1502,6 +1502,8 @@ You can pass it data in __init__(), as a dictionary.
Pass a dictionary to a Form's __init__(). Pass a dictionary to a Form's __init__().
>>> p = Person({'first_name': u'John', 'last_name': u'Lennon', 'birthday': u'1940-10-9'}) >>> p = Person({'first_name': u'John', 'last_name': u'Lennon', 'birthday': u'1940-10-9'})
>>> p.is_bound
True
>>> p.errors >>> p.errors
{} {}
>>> p.is_valid() >>> p.is_valid()
@ -1540,6 +1542,8 @@ Birthday 1940-10-9
Empty dictionaries are valid, too. Empty dictionaries are valid, too.
>>> p = Person({}) >>> p = Person({})
>>> p.is_bound
True
>>> p.errors >>> p.errors
{'first_name': [u'This field is required.'], 'last_name': [u'This field is required.'], 'birthday': [u'This field is required.']} {'first_name': [u'This field is required.'], 'last_name': [u'This field is required.'], 'birthday': [u'This field is required.']}
>>> p.is_valid() >>> p.is_valid()
@ -1569,9 +1573,11 @@ AttributeError: 'birthday' object has no attribute 'clean_data'
<p><label for="id_birthday">Birthday:</label> <input type="text" name="birthday" id="id_birthday" /></p> <p><label for="id_birthday">Birthday:</label> <input type="text" name="birthday" id="id_birthday" /></p>
If you don't pass any values to the Form's __init__(), or if you pass None, If you don't pass any values to the Form's __init__(), or if you pass None,
the Form won't do any validation. Form.errors will be an empty dictionary *but* the Form will be considered unbound and won't do any validation. Form.errors
Form.is_valid() will return False. will be an empty dictionary *but* Form.is_valid() will return False.
>>> p = Person() >>> p = Person()
>>> p.is_bound
False
>>> p.errors >>> p.errors
{} {}
>>> p.is_valid() >>> p.is_valid()