forms.txt 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692
  1. .. _obsolete-forms:
  2. ===============================
  3. Forms, fields, and manipulators
  4. ===============================
  5. Forwards-compatibility note
  6. ===========================
  7. The legacy forms/manipulators system described in this document is going to be
  8. replaced in the next Django release. If you're starting from scratch, we
  9. strongly encourage you not to waste your time learning this. Instead, learn and
  10. use the new :ref:`forms library <topics-forms-index>`.
  11. Introduction
  12. ============
  13. Once you've got a chance to play with Django's admin interface, you'll probably
  14. wonder if the fantastic form validation framework it uses is available to user
  15. code. It is, and this document explains how the framework works.
  16. We'll take a top-down approach to examining Django's form validation framework,
  17. because much of the time you won't need to use the lower-level APIs. Throughout
  18. this document, we'll be working with the following model, a "place" object::
  19. from django.db import models
  20. PLACE_TYPES = (
  21. (1, 'Bar'),
  22. (2, 'Restaurant'),
  23. (3, 'Movie Theater'),
  24. (4, 'Secret Hideout'),
  25. )
  26. class Place(models.Model):
  27. name = models.CharField(max_length=100)
  28. address = models.CharField(max_length=100, blank=True)
  29. city = models.CharField(max_length=50, blank=True)
  30. state = models.USStateField()
  31. zip_code = models.CharField(max_length=5, blank=True)
  32. place_type = models.IntegerField(choices=PLACE_TYPES)
  33. class Admin:
  34. pass
  35. def __unicode__(self):
  36. return self.name
  37. Defining the above class is enough to create an admin interface to a ``Place``,
  38. but what if you want to allow public users to submit places?
  39. Automatic Manipulators
  40. ======================
  41. The highest-level interface for object creation and modification is the
  42. **automatic Manipulator** framework. An automatic manipulator is a utility
  43. class tied to a given model that "knows" how to create or modify instances of
  44. that model and how to validate data for the object. Automatic Manipulators come
  45. in two flavors: ``AddManipulators`` and ``ChangeManipulators``. Functionally
  46. they are quite similar, but the former knows how to create new instances of the
  47. model, while the latter modifies existing instances. Both types of classes are
  48. automatically created when you define a new class::
  49. >>> from mysite.myapp.models import Place
  50. >>> Place.AddManipulator
  51. <class 'django.models.manipulators.AddManipulator'>
  52. >>> Place.ChangeManipulator
  53. <class 'django.models.manipulators.ChangeManipulator'>
  54. Using the ``AddManipulator``
  55. ----------------------------
  56. We'll start with the ``AddManipulator``. Here's a very simple view that takes
  57. POSTed data from the browser and creates a new ``Place`` object::
  58. from django.shortcuts import render_to_response
  59. from django.http import Http404, HttpResponse, HttpResponseRedirect
  60. from django import oldforms as forms
  61. from mysite.myapp.models import Place
  62. def naive_create_place(request):
  63. """A naive approach to creating places; don't actually use this!"""
  64. # Create the AddManipulator.
  65. manipulator = Place.AddManipulator()
  66. # Make a copy of the POSTed data so that do_html2python can
  67. # modify it in place (request.POST is immutable).
  68. new_data = request.POST.copy()
  69. # Convert the request data (which will all be strings) into the
  70. # appropriate Python types for those fields.
  71. manipulator.do_html2python(new_data)
  72. # Save the new object.
  73. new_place = manipulator.save(new_data)
  74. # It worked!
  75. return HttpResponse("Place created: %s" % new_place)
  76. The ``naive_create_place`` example works, but as you probably can tell, this
  77. view has a number of problems:
  78. * No validation of any sort is performed. If, for example, the ``name`` field
  79. isn't given in ``request.POST``, the save step will cause a database error
  80. because that field is required. Ugly.
  81. * Even if you *do* perform validation, there's still no way to give that
  82. information to the user in any sort of useful way.
  83. * You'll have to separately create a form (and view) that submits to this
  84. page, which is a pain and is redundant.
  85. Let's dodge these problems momentarily to take a look at how you could create a
  86. view with a form that submits to this flawed creation view::
  87. def naive_create_place_form(request):
  88. """Simplistic place form view; don't actually use anything like this!"""
  89. # Create a FormWrapper object that the template can use. Ignore
  90. # the last two arguments to FormWrapper for now.
  91. form = forms.FormWrapper(Place.AddManipulator(), {}, {})
  92. return render_to_response('places/naive_create_form.html', {'form': form})
  93. (This view, as well as all the following ones, has the same imports as in the
  94. first example above.)
  95. The ``forms.FormWrapper`` object is a wrapper that templates can
  96. easily deal with to create forms. Here's the ``naive_create_form.html``
  97. template::
  98. {% extends "base.html" %}
  99. {% block content %}
  100. <h1>Create a place:</h1>
  101. <form method="post" action="../do_new/">
  102. <p><label for="id_name">Name:</label> {{ form.name }}</p>
  103. <p><label for="id_address">Address:</label> {{ form.address }}</p>
  104. <p><label for="id_city">City:</label> {{ form.city }}</p>
  105. <p><label for="id_state">State:</label> {{ form.state }}</p>
  106. <p><label for="id_zip_code">Zip:</label> {{ form.zip_code }}</p>
  107. <p><label for="id_place_type">Place type:</label> {{ form.place_type }}</p>
  108. <input type="submit" />
  109. </form>
  110. {% endblock %}
  111. Before we get back to the problems with these naive set of views, let's go over
  112. some salient points of the above template:
  113. * Field "widgets" are handled for you: ``{{ form.field }}`` automatically
  114. creates the "right" type of widget for the form, as you can see with the
  115. ``place_type`` field above.
  116. * There isn't a way just to spit out the form. You'll still need to define
  117. how the form gets laid out. This is a feature: Every form should be
  118. designed differently. Django doesn't force you into any type of mold.
  119. If you must use tables, use tables. If you're a semantic purist, you can
  120. probably find better HTML than in the above template.
  121. * To avoid name conflicts, the ``id`` values of form elements take the
  122. form "id_*fieldname*".
  123. By creating a creation form we've solved problem number 3 above, but we still
  124. don't have any validation. Let's revise the validation issue by writing a new
  125. creation view that takes validation into account::
  126. def create_place_with_validation(request):
  127. manipulator = Place.AddManipulator()
  128. new_data = request.POST.copy()
  129. # Check for validation errors
  130. errors = manipulator.get_validation_errors(new_data)
  131. manipulator.do_html2python(new_data)
  132. if errors:
  133. return render_to_response('places/errors.html', {'errors': errors})
  134. else:
  135. new_place = manipulator.save(new_data)
  136. return HttpResponse("Place created: %s" % new_place)
  137. In this new version, errors will be found -- ``manipulator.get_validation_errors``
  138. handles all the validation for you -- and those errors can be nicely presented
  139. on an error page (templated, of course)::
  140. {% extends "base.html" %}
  141. {% block content %}
  142. <h1>Please go back and correct the following error{{ errors|pluralize }}:</h1>
  143. <ul>
  144. {% for e in errors.items %}
  145. <li>Field "{{ e.0 }}": {{ e.1|join:", " }}</li>
  146. {% endfor %}
  147. </ul>
  148. {% endblock %}
  149. Still, this has its own problems:
  150. * There's still the issue of creating a separate (redundant) view for the
  151. submission form.
  152. * Errors, though nicely presented, are on a separate page, so the user will
  153. have to use the "back" button to fix errors. That's ridiculous and unusable.
  154. The best way to deal with these issues is to collapse the two views -- the form
  155. and the submission -- into a single view. This view will be responsible for
  156. creating the form, validating POSTed data, and creating the new object (if the
  157. data is valid). An added bonus of this approach is that errors and the form will
  158. both be available on the same page, so errors with fields can be presented in
  159. context.
  160. .. admonition:: Philosophy:
  161. Finally, for the HTTP purists in the audience (and the authorship), this
  162. nicely matches the "true" meanings of HTTP GET and HTTP POST: GET fetches
  163. the form, and POST creates the new object.
  164. Below is the finished view::
  165. def create_place(request):
  166. manipulator = Place.AddManipulator()
  167. if request.method == 'POST':
  168. # If data was POSTed, we're trying to create a new Place.
  169. new_data = request.POST.copy()
  170. # Check for errors.
  171. errors = manipulator.get_validation_errors(new_data)
  172. manipulator.do_html2python(new_data)
  173. if not errors:
  174. # No errors. This means we can save the data!
  175. new_place = manipulator.save(new_data)
  176. # Redirect to the object's "edit" page. Always use a redirect
  177. # after POST data, so that reloads don't accidentally create
  178. # duplicate entries, and so users don't see the confusing
  179. # "Repost POST data?" alert box in their browsers.
  180. return HttpResponseRedirect("/places/edit/%i/" % new_place.id)
  181. else:
  182. # No POST, so we want a brand new form without any data or errors.
  183. errors = new_data = {}
  184. # Create the FormWrapper, template, context, response.
  185. form = forms.FormWrapper(manipulator, new_data, errors)
  186. return render_to_response('places/create_form.html', {'form': form})
  187. and here's the ``create_form`` template::
  188. {% extends "base.html" %}
  189. {% block content %}
  190. <h1>Create a place:</h1>
  191. {% if form.has_errors %}
  192. <h2>Please correct the following error{{ form.error_dict|pluralize }}:</h2>
  193. {% endif %}
  194. <form method="post" action=".">
  195. <p>
  196. <label for="id_name">Name:</label> {{ form.name }}
  197. {% if form.name.errors %}*** {{ form.name.errors|join:", " }}{% endif %}
  198. </p>
  199. <p>
  200. <label for="id_address">Address:</label> {{ form.address }}
  201. {% if form.address.errors %}*** {{ form.address.errors|join:", " }}{% endif %}
  202. </p>
  203. <p>
  204. <label for="id_city">City:</label> {{ form.city }}
  205. {% if form.city.errors %}*** {{ form.city.errors|join:", " }}{% endif %}
  206. </p>
  207. <p>
  208. <label for="id_state">State:</label> {{ form.state }}
  209. {% if form.state.errors %}*** {{ form.state.errors|join:", " }}{% endif %}
  210. </p>
  211. <p>
  212. <label for="id_zip_code">Zip:</label> {{ form.zip_code }}
  213. {% if form.zip_code.errors %}*** {{ form.zip_code.errors|join:", " }}{% endif %}
  214. </p>
  215. <p>
  216. <label for="id_place_type">Place type:</label> {{ form.place_type }}
  217. {% if form.place_type.errors %}*** {{ form.place_type.errors|join:", " }}{% endif %}
  218. </p>
  219. <input type="submit" />
  220. </form>
  221. {% endblock %}
  222. The second two arguments to ``FormWrapper`` (``new_data`` and ``errors``)
  223. deserve some mention.
  224. The first is any "default" data to be used as values for the fields. Pulling
  225. the data from ``request.POST``, as is done above, makes sure that if there are
  226. errors, the values the user put in aren't lost. If you try the above example,
  227. you'll see this in action.
  228. The second argument is the error list retrieved from
  229. ``manipulator.get_validation_errors``. When passed into the ``FormWrapper``,
  230. this gives each field an ``errors`` item (which is a list of error messages
  231. associated with the field) as well as a ``html_error_list`` item, which is a
  232. ``<ul>`` of error messages. The above template uses these error items to
  233. display a simple error message next to each field. The error list is saved as
  234. an ``error_dict`` attribute of the ``FormWrapper`` object.
  235. Using the ``ChangeManipulator``
  236. -------------------------------
  237. The above has covered using the ``AddManipulator`` to create a new object. What
  238. about editing an existing one? It's shockingly similar to creating a new one::
  239. def edit_place(request, place_id):
  240. # Get the place in question from the database and create a
  241. # ChangeManipulator at the same time.
  242. try:
  243. manipulator = Place.ChangeManipulator(place_id)
  244. except Place.DoesNotExist:
  245. raise Http404
  246. # Grab the Place object in question for future use.
  247. place = manipulator.original_object
  248. if request.method == 'POST':
  249. new_data = request.POST.copy()
  250. errors = manipulator.get_validation_errors(new_data)
  251. manipulator.do_html2python(new_data)
  252. if not errors:
  253. manipulator.save(new_data)
  254. # Do a post-after-redirect so that reload works, etc.
  255. return HttpResponseRedirect("/places/edit/%i/" % place.id)
  256. else:
  257. errors = {}
  258. # This makes sure the form accurate represents the fields of the place.
  259. new_data = manipulator.flatten_data()
  260. form = forms.FormWrapper(manipulator, new_data, errors)
  261. return render_to_response('places/edit_form.html', {'form': form, 'place': place})
  262. The only real differences are:
  263. * We create a ``ChangeManipulator`` instead of an ``AddManipulator``.
  264. The argument to a ``ChangeManipulator`` is the ID of the object
  265. to be changed. As you can see, the initializer will raise an
  266. ``ObjectDoesNotExist`` exception if the ID is invalid.
  267. * ``ChangeManipulator.original_object`` stores the instance of the
  268. object being edited.
  269. * We set ``new_data`` based upon ``flatten_data()`` from the manipulator.
  270. ``flatten_data()`` takes the data from the original object under
  271. manipulation, and converts it into a data dictionary that can be used
  272. to populate form elements with the existing values for the object.
  273. * The above example uses a different template, so create and edit can be
  274. "skinned" differently if needed, but the form chunk itself is completely
  275. identical to the one in the create form above.
  276. The astute programmer will notice the add and create functions are nearly
  277. identical and could in fact be collapsed into a single view. This is left as an
  278. exercise for said programmer.
  279. (However, the even-more-astute programmer will take heed of the note at the top
  280. of this document and check out the :ref:`generic views <ref-generic-views>`
  281. documentation if all she wishes to do is this type of simple create/update.)
  282. Custom forms and manipulators
  283. =============================
  284. All the above is fine and dandy if you just want to use the automatically
  285. created manipulators. But the coolness doesn't end there: You can easily create
  286. your own custom manipulators for handling custom forms.
  287. Custom manipulators are pretty simple. Here's a manipulator that you might use
  288. for a "contact" form on a website::
  289. from django import oldforms as forms
  290. urgency_choices = (
  291. (1, "Extremely urgent"),
  292. (2, "Urgent"),
  293. (3, "Normal"),
  294. (4, "Unimportant"),
  295. )
  296. class ContactManipulator(forms.Manipulator):
  297. def __init__(self):
  298. self.fields = (
  299. forms.EmailField(field_name="from", is_required=True),
  300. forms.TextField(field_name="subject", length=30, max_length=200, is_required=True),
  301. forms.SelectField(field_name="urgency", choices=urgency_choices),
  302. forms.LargeTextField(field_name="contents", is_required=True),
  303. )
  304. A certain similarity to Django's models should be apparent. The only required
  305. method of a custom manipulator is ``__init__`` which must define the fields
  306. present in the manipulator. See the ``django.forms`` module for
  307. all the form fields provided by Django.
  308. You use this custom manipulator exactly as you would use an auto-generated one.
  309. Here's a simple function that might drive the above form::
  310. def contact_form(request):
  311. manipulator = ContactManipulator()
  312. if request.method == 'POST':
  313. new_data = request.POST.copy()
  314. errors = manipulator.get_validation_errors(new_data)
  315. manipulator.do_html2python(new_data)
  316. if not errors:
  317. # Send e-mail using new_data here...
  318. return HttpResponseRedirect("/contact/thankyou/")
  319. else:
  320. errors = new_data = {}
  321. form = forms.FormWrapper(manipulator, new_data, errors)
  322. return render_to_response('contact_form.html', {'form': form})
  323. Implementing ``flatten_data`` for custom manipulators
  324. ------------------------------------------------------
  325. It is possible (although rarely needed) to replace the default automatically
  326. created manipulators on a model with your own custom manipulators. If you do
  327. this and you are intending to use those models in generic views, you should
  328. also define a ``flatten_data`` method in any ``ChangeManipulator`` replacement.
  329. This should act like the default ``flatten_data`` and return a dictionary
  330. mapping field names to their values, like so::
  331. def flatten_data(self):
  332. obj = self.original_object
  333. return dict(
  334. from = obj.from,
  335. subject = obj.subject,
  336. ...
  337. )
  338. In this way, your new change manipulator will act exactly like the default
  339. version.
  340. ``FileField`` and ``ImageField`` special cases
  341. ==============================================
  342. Dealing with ``FileField`` and ``ImageField`` objects is a little more
  343. complicated.
  344. First, you'll need to make sure that your ``<form>`` element correctly defines
  345. the ``enctype`` as ``"multipart/form-data"``, in order to upload files::
  346. <form enctype="multipart/form-data" method="post" action="/foo/">
  347. Next, you'll need to treat the field in the template slightly differently. A
  348. ``FileField`` or ``ImageField`` is represented by *two* HTML form elements.
  349. For example, given this field in a model::
  350. photo = model.ImageField('/path/to/upload/location')
  351. You'd need to display two formfields in the template::
  352. <p><label for="id_photo">Photo:</label> {{ form.photo }}{{ form.photo_file }}</p>
  353. The first bit (``{{ form.photo }}``) displays the currently-selected file,
  354. while the second (``{{ form.photo_file }}``) actually contains the file upload
  355. form field. Thus, at the validation layer you need to check the ``photo_file``
  356. key.
  357. Finally, in your view, make sure to access ``request.FILES``, rather than
  358. ``request.POST``, for the uploaded files. This is necessary because
  359. ``request.POST`` does not contain file-upload data.
  360. For example, following the ``new_data`` convention, you might do something like
  361. this::
  362. new_data = request.POST.copy()
  363. new_data.update(request.FILES)
  364. Validators
  365. ==========
  366. One useful feature of manipulators is the automatic validation. Validation is
  367. done using a simple validation API: A validator is a callable that raises a
  368. ``ValidationError`` if there's something wrong with the data.
  369. ``django.core.validators`` defines a host of validator functions (see below),
  370. but defining your own couldn't be easier::
  371. from django.core import validators
  372. from django import oldforms as forms
  373. class ContactManipulator(forms.Manipulator):
  374. def __init__(self):
  375. self.fields = (
  376. # ... snip fields as above ...
  377. forms.EmailField(field_name="to", validator_list=[self.isValidToAddress])
  378. )
  379. def isValidToAddress(self, field_data, all_data):
  380. if not field_data.endswith("@example.com"):
  381. raise validators.ValidationError("You can only send messages to example.com e-mail addresses.")
  382. Above, we've added a "to" field to the contact form, but required that the "to"
  383. address end with "@example.com" by adding the ``isValidToAddress`` validator to
  384. the field's ``validator_list``.
  385. The arguments to a validator function take a little explanation. ``field_data``
  386. is the value of the field in question, and ``all_data`` is a dictionary of all
  387. the data being validated.
  388. .. admonition:: Note::
  389. At the point validators are called all data will still be
  390. strings (as ``do_html2python`` hasn't been called yet).
  391. Also, because consistency in user interfaces is important, we strongly urge you
  392. to put punctuation at the end of your validation messages.
  393. When are validators called?
  394. ---------------------------
  395. After a form has been submitted, Django validates each field in turn. First,
  396. if the field is required, Django checks that it is present and non-empty. Then,
  397. if that test passes *and the form submission contained data* for that field, all
  398. the validators for that field are called in turn. The emphasized portion in the
  399. last sentence is important: if a form field is not submitted (because it
  400. contains no data -- which is normal HTML behavior), the validators are not
  401. run against the field.
  402. This feature is particularly important for models using
  403. ``models.BooleanField`` or custom manipulators using things like
  404. ``forms.CheckBoxField``. If the checkbox is not selected, it will not
  405. contribute to the form submission.
  406. If you would like your validator to run *always*, regardless of whether its
  407. attached field contains any data, set the ``always_test`` attribute on the
  408. validator function. For example::
  409. def my_custom_validator(field_data, all_data):
  410. # ...
  411. my_custom_validator.always_test = True
  412. This validator will always be executed for any field it is attached to.
  413. Ready-made validators
  414. ---------------------
  415. Writing your own validator is not difficult, but there are some situations
  416. that come up over and over again. Django comes with a number of validators
  417. that can be used directly in your code. All of these functions and classes
  418. reside in ``django/core/validators.py``.
  419. The following validators should all be self-explanatory. Each one provides a
  420. check for the given property:
  421. * isAlphaNumeric
  422. * isAlphaNumericURL
  423. * isSlug
  424. * isLowerCase
  425. * isUpperCase
  426. * isCommaSeparatedIntegerList
  427. * isCommaSeparatedEmailList
  428. * isValidIPAddress4
  429. * isNotEmpty
  430. * isOnlyDigits
  431. * isNotOnlyDigits
  432. * isInteger
  433. * isOnlyLetters
  434. * isValidANSIDate
  435. * isValidANSITime
  436. * isValidEmail
  437. * isValidFloat
  438. * isValidImage
  439. * isValidImageURL
  440. * isValidPhone
  441. * isValidQuicktimeVideoURL
  442. * isValidURL
  443. * isValidHTML
  444. * isWellFormedXml
  445. * isWellFormedXmlFragment
  446. * isExistingURL
  447. * isValidUSState
  448. * hasNoProfanities
  449. There are also a group of validators that are slightly more flexible. For
  450. these validators, you create a validator instance, passing in the parameters
  451. described below. The returned object is a callable that can be used as a
  452. validator.
  453. For example::
  454. from django.core import validators
  455. from django import oldforms as forms
  456. power_validator = validators.IsAPowerOf(2)
  457. class InstallationManipulator(forms.Manipulator)
  458. def __init__(self):
  459. self.fields = (
  460. ...
  461. forms.IntegerField(field_name = "size", validator_list=[power_validator])
  462. )
  463. Here, ``validators.IsAPowerOf(...)`` returned something that could be used as
  464. a validator (in this case, a check that a number was a power of 2).
  465. Each of the standard validators that take parameters have an optional final
  466. argument (``error_message``) that is the message returned when validation
  467. fails. If no message is passed in, a default message is used.
  468. ``AlwaysMatchesOtherField``
  469. Takes a field name and the current field is valid if and only if its value
  470. matches the contents of the other field.
  471. ``ValidateIfOtherFieldEquals``
  472. Takes three parameters: ``other_field``, ``other_value`` and
  473. ``validator_list``, in that order. If ``other_field`` has a value of
  474. ``other_value``, then the validators in ``validator_list`` are all run
  475. against the current field.
  476. ``RequiredIfOtherFieldGiven``
  477. Takes a field name of the current field is only required if the other
  478. field has a value.
  479. ``RequiredIfOtherFieldsGiven``
  480. Similar to ``RequiredIfOtherFieldGiven``, except that it takes a list of
  481. field names and if any one of the supplied fields has a value provided,
  482. the current field being validated is required.
  483. ``RequiredIfOtherFieldNotGiven``
  484. Takes the name of the other field and this field is only required if the
  485. other field has no value.
  486. ``RequiredIfOtherFieldEquals`` and ``RequiredIfOtherFieldDoesNotEqual``
  487. Each of these validator classes takes a field name and a value (in that
  488. order). If the given field does (or does not have, in the latter case) the
  489. given value, then the current field being validated is required.
  490. An optional ``other_label`` argument can be passed which, if given, is used
  491. in error messages instead of the value. This allows more user friendly error
  492. messages if the value itself is not descriptive enough.
  493. Note that because validators are called before any ``do_html2python()``
  494. functions, the value being compared against is a string. So
  495. ``RequiredIfOtherFieldEquals('choice', '1')`` is correct, whilst
  496. ``RequiredIfOtherFieldEquals('choice', 1)`` will never result in the
  497. equality test succeeding.
  498. ``IsLessThanOtherField``
  499. Takes a field name and validates that the current field being validated
  500. has a value that is less than (or equal to) the other field's value.
  501. Again, comparisons are done using strings, so be cautious about using
  502. this function to compare data that should be treated as another type. The
  503. string "123" is less than the string "2", for example. If you don't want
  504. string comparison here, you will need to write your own validator.
  505. ``NumberIsInRange``
  506. Takes two boundary numbers, ``lower`` and ``upper``, and checks that the
  507. field is greater than ``lower`` (if given) and less than ``upper`` (if
  508. given).
  509. Both checks are inclusive. That is, ``NumberIsInRange(10, 20)`` will allow
  510. values of both 10 and 20. This validator only checks numeric values
  511. (e.g., float and integer values).
  512. ``IsAPowerOf``
  513. Takes an integer argument and when called as a validator, checks that the
  514. field being validated is a power of the integer.
  515. ``IsValidDecimal``
  516. Takes a maximum number of digits and number of decimal places (in that
  517. order) and validates whether the field is a decimal with no more than the
  518. maximum number of digits and decimal places.
  519. ``MatchesRegularExpression``
  520. Takes a regular expression (a string) as a parameter and validates the
  521. field value against it.
  522. ``AnyValidator``
  523. Takes a list of validators as a parameter. At validation time, if the
  524. field successfully validates against any one of the validators, it passes
  525. validation. The validators are tested in the order specified in the
  526. original list.
  527. ``URLMimeTypeCheck``
  528. Used to validate URL fields. Takes a list of MIME types (such as
  529. ``text/plain``) at creation time. At validation time, it verifies that the
  530. field is indeed a URL and then tries to retrieve the content at the URL.
  531. Validation succeeds if the content could be retrieved and it has a content
  532. type from the list used to create the validator.
  533. ``RelaxNGCompact``
  534. Used to validate an XML document against a Relax NG compact schema. Takes a
  535. file path to the location of the schema and an optional root element (which
  536. is wrapped around the XML fragment before validation, if supplied). At
  537. validation time, the XML fragment is validated against the schema using the
  538. executable specified in the ``JING_PATH`` setting (see the :ref:`settings
  539. <ref-settings>` document for more details).