newforms: Changed Form so that clean_data only exists if a Form is valid. Thanks for the idea, Honza Kral
git-svn-id: http://code.djangoproject.com/svn/django/trunk@4284 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
parent
ddb9b7d57a
commit
b1f6b376c0
|
@ -44,7 +44,6 @@ class BaseForm(StrAndUnicode):
|
||||||
self.data = data or {}
|
self.data = data or {}
|
||||||
self.auto_id = auto_id
|
self.auto_id = auto_id
|
||||||
self.prefix = prefix
|
self.prefix = prefix
|
||||||
self.clean_data = None # Stores the data after clean() has been called.
|
|
||||||
self.__errors = None # Stores the errors after clean() has been called.
|
self.__errors = None # Stores the errors after clean() has been called.
|
||||||
|
|
||||||
def __unicode__(self):
|
def __unicode__(self):
|
||||||
|
@ -137,11 +136,11 @@ 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.
|
||||||
"""
|
"""
|
||||||
self.clean_data = {}
|
|
||||||
errors = ErrorDict()
|
errors = ErrorDict()
|
||||||
if self.ignore_errors: # Stop further processing.
|
if self.ignore_errors: # Stop further processing.
|
||||||
self.__errors = errors
|
self.__errors = errors
|
||||||
return
|
return
|
||||||
|
self.clean_data = {}
|
||||||
for name, field in self.fields.items():
|
for name, field in self.fields.items():
|
||||||
# value_from_datadict() gets the data from the dictionary.
|
# value_from_datadict() gets the data from the dictionary.
|
||||||
# Each widget type knows how to retrieve its own data, because some
|
# Each widget type knows how to retrieve its own data, because some
|
||||||
|
@ -160,7 +159,7 @@ class BaseForm(StrAndUnicode):
|
||||||
except ValidationError, e:
|
except ValidationError, e:
|
||||||
errors[NON_FIELD_ERRORS] = e.messages
|
errors[NON_FIELD_ERRORS] = e.messages
|
||||||
if errors:
|
if errors:
|
||||||
self.clean_data = None
|
delattr(self, 'clean_data')
|
||||||
self.__errors = errors
|
self.__errors = errors
|
||||||
|
|
||||||
def clean(self):
|
def clean(self):
|
||||||
|
|
|
@ -1544,6 +1544,10 @@ Empty dictionaries are valid, too.
|
||||||
{'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()
|
||||||
False
|
False
|
||||||
|
>>> p.clean_data
|
||||||
|
Traceback (most recent call last):
|
||||||
|
...
|
||||||
|
AttributeError: 'birthday' object has no attribute 'clean_data'
|
||||||
>>> print p
|
>>> print p
|
||||||
<tr><th><label for="id_first_name">First name:</label></th><td><ul class="errorlist"><li>This field is required.</li></ul><input type="text" name="first_name" id="id_first_name" /></td></tr>
|
<tr><th><label for="id_first_name">First name:</label></th><td><ul class="errorlist"><li>This field is required.</li></ul><input type="text" name="first_name" id="id_first_name" /></td></tr>
|
||||||
<tr><th><label for="id_last_name">Last name:</label></th><td><ul class="errorlist"><li>This field is required.</li></ul><input type="text" name="last_name" id="id_last_name" /></td></tr>
|
<tr><th><label for="id_last_name">Last name:</label></th><td><ul class="errorlist"><li>This field is required.</li></ul><input type="text" name="last_name" id="id_last_name" /></td></tr>
|
||||||
|
@ -1572,6 +1576,10 @@ Form.is_valid() will return False.
|
||||||
{}
|
{}
|
||||||
>>> p.is_valid()
|
>>> p.is_valid()
|
||||||
False
|
False
|
||||||
|
>>> p.clean_data
|
||||||
|
Traceback (most recent call last):
|
||||||
|
...
|
||||||
|
AttributeError: 'birthday' object has no attribute 'clean_data'
|
||||||
>>> print p
|
>>> print p
|
||||||
<tr><th><label for="id_first_name">First name:</label></th><td><input type="text" name="first_name" id="id_first_name" /></td></tr>
|
<tr><th><label for="id_first_name">First name:</label></th><td><input type="text" name="first_name" id="id_first_name" /></td></tr>
|
||||||
<tr><th><label for="id_last_name">Last name:</label></th><td><input type="text" name="last_name" id="id_last_name" /></td></tr>
|
<tr><th><label for="id_last_name">Last name:</label></th><td><input type="text" name="last_name" id="id_last_name" /></td></tr>
|
||||||
|
@ -1611,8 +1619,9 @@ u'<ul class="errorlist"><li>first_name<ul class="errorlist"><li>This field is re
|
||||||
* birthday
|
* birthday
|
||||||
* This field is required.
|
* This field is required.
|
||||||
>>> p.clean_data
|
>>> p.clean_data
|
||||||
>>> repr(p.clean_data)
|
Traceback (most recent call last):
|
||||||
'None'
|
...
|
||||||
|
AttributeError: 'birthday' object has no attribute 'clean_data'
|
||||||
>>> p['first_name'].errors
|
>>> p['first_name'].errors
|
||||||
[u'This field is required.']
|
[u'This field is required.']
|
||||||
>>> p['first_name'].errors.as_ul()
|
>>> p['first_name'].errors.as_ul()
|
||||||
|
|
Loading…
Reference in New Issue