summaryrefslogtreecommitdiff
path: root/docs/forms.txt
diff options
context:
space:
mode:
Diffstat (limited to 'docs/forms.txt')
-rw-r--r--docs/forms.txt74
1 files changed, 52 insertions, 22 deletions
diff --git a/docs/forms.txt b/docs/forms.txt
index 6efa24d28f..f6cb55a3f6 100644
--- a/docs/forms.txt
+++ b/docs/forms.txt
@@ -14,7 +14,7 @@ use the django.newforms system, which we have begun to document in the
If you have legacy form/manipulator code, read the "Migration plan" section in
that document to understand how we're making the switch.
-.. _newforms documentation: http://www.djangoproject.com/documentation/newforms/
+.. _newforms documentation: ../newforms/
Introduction
============
@@ -173,10 +173,10 @@ creation view that takes validation into account::
# Check for validation errors
errors = manipulator.get_validation_errors(new_data)
+ manipulator.do_html2python(new_data)
if errors:
return render_to_response('places/errors.html', {'errors': errors})
else:
- manipulator.do_html2python(new_data)
new_place = manipulator.save(new_data)
return HttpResponse("Place created: %s" % new_place)
@@ -229,10 +229,10 @@ Below is the finished view::
# Check for errors.
errors = manipulator.get_validation_errors(new_data)
+ manipulator.do_html2python(new_data)
if not errors:
# No errors. This means we can save the data!
- manipulator.do_html2python(new_data)
new_place = manipulator.save(new_data)
# Redirect to the object's "edit" page. Always use a redirect
@@ -324,8 +324,8 @@ about editing an existing one? It's shockingly similar to creating a new one::
if request.method == 'POST':
new_data = request.POST.copy()
errors = manipulator.get_validation_errors(new_data)
+ manipulator.do_html2python(new_data)
if not errors:
- manipulator.do_html2python(new_data)
manipulator.save(new_data)
# Do a post-after-redirect so that reload works, etc.
@@ -406,8 +406,8 @@ Here's a simple function that might drive the above form::
if request.method == 'POST':
new_data = request.POST.copy()
errors = manipulator.get_validation_errors(new_data)
+ manipulator.do_html2python(new_data)
if not errors:
- manipulator.do_html2python(new_data)
# Send e-mail using new_data here...
@@ -417,6 +417,27 @@ Here's a simple function that might drive the above form::
form = forms.FormWrapper(manipulator, new_data, errors)
return render_to_response('contact_form.html', {'form': form})
+Implementing ``flatten_data`` for custom manipulators
+------------------------------------------------------
+
+It is possible (although rarely needed) to replace the default automatically
+created manipulators on a model with your own custom manipulators. If you do
+this and you are intending to use those models in generic views, you should
+also define a ``flatten_data`` method in any ``ChangeManipulator`` replacement.
+This should act like the default ``flatten_data`` and return a dictionary
+mapping field names to their values, like so::
+
+ def flatten_data(self):
+ obj = self.original_object
+ return dict(
+ from = obj.from,
+ subject = obj.subject,
+ ...
+ )
+
+In this way, your new change manipulator will act exactly like the default
+version.
+
``FileField`` and ``ImageField`` special cases
==============================================
@@ -496,10 +517,10 @@ to put punctuation at the end of your validation messages.
When are validators called?
---------------------------
-After a form has been submitted, Django first checks to see that all the
-required fields are present and non-empty. For each field that passes that
-test *and if the form submission contained data* for that field, all the
-validators for that field are called in turn. The emphasized portion in the
+After a form has been submitted, Django validates each field in turn. First,
+if the field is required, Django checks that it is present and non-empty. Then,
+if that test passes *and the form submission contained data* for that field, all
+the validators for that field are called in turn. The emphasized portion in the
last sentence is important: if a form field is not submitted (because it
contains no data -- which is normal HTML behavior), the validators are not
run against the field.
@@ -546,6 +567,7 @@ check for the given property:
* isValidANSIDate
* isValidANSITime
* isValidEmail
+ * isValidFloat
* isValidImage
* isValidImageURL
* isValidPhone
@@ -594,20 +616,28 @@ fails. If no message is passed in, a default message is used.
``other_value``, then the validators in ``validator_list`` are all run
against the current field.
+``RequiredIfOtherFieldGiven``
+ Takes a field name of the current field is only required if the other
+ field has a value.
+
+``RequiredIfOtherFieldsGiven``
+ Similar to ``RequiredIfOtherFieldGiven``, except that it takes a list of
+ field names and if any one of the supplied fields has a value provided,
+ the current field being validated is required.
+
``RequiredIfOtherFieldNotGiven``
Takes the name of the other field and this field is only required if the
other field has no value.
-``RequiredIfOtherFieldsNotGiven``
- Similar to ``RequiredIfOtherFieldNotGiven``, except that it takes a list
- of field names and if any one of the supplied fields does not have a value
- provided, the field being validated is required.
-
``RequiredIfOtherFieldEquals`` and ``RequiredIfOtherFieldDoesNotEqual``
Each of these validator classes takes a field name and a value (in that
order). If the given field does (or does not have, in the latter case) the
given value, then the current field being validated is required.
+ An optional ``other_label`` argument can be passed which, if given, is used
+ in error messages instead of the value. This allows more user friendly error
+ messages if the value itself is not descriptive enough.
+
Note that because validators are called before any ``do_html2python()``
functions, the value being compared against is a string. So
``RequiredIfOtherFieldEquals('choice', '1')`` is correct, whilst
@@ -625,8 +655,8 @@ fails. If no message is passed in, a default message is used.
``NumberIsInRange``
Takes two boundary numbers, ``lower`` and ``upper``, and checks that the
field is greater than ``lower`` (if given) and less than ``upper`` (if
- given).
-
+ given).
+
Both checks are inclusive. That is, ``NumberIsInRange(10, 20)`` will allow
values of both 10 and 20. This validator only checks numeric values
(e.g., float and integer values).
@@ -635,10 +665,10 @@ fails. If no message is passed in, a default message is used.
Takes an integer argument and when called as a validator, checks that the
field being validated is a power of the integer.
-``IsValidFloat``
+``IsValidDecimal``
Takes a maximum number of digits and number of decimal places (in that
- order) and validates whether the field is a float with less than the
- maximum number of digits and decimal place.
+ order) and validates whether the field is a decimal with no more than the
+ maximum number of digits and decimal places.
``MatchesRegularExpression``
Takes a regular expression (a string) as a parameter and validates the
@@ -665,6 +695,6 @@ fails. If no message is passed in, a default message is used.
the executable specified in the ``JING_PATH`` setting (see the settings_
document for more details).
-.. _`generic views`: http://www.djangoproject.com/documentation/generic_views/
-.. _`models API`: http://www.djangoproject.com/documentation/model_api/
-.. _settings: http://www.djangoproject.com/documentation/settings/
+.. _`generic views`: ../generic_views/
+.. _`models API`: ../model-api/
+.. _settings: ../settings/