Warning:
Can't synchronize with repository "(default)" (Unsupported version control system "svn": No module named svn). Look in the Trac log for more information.
| File SimpleWidgetForm.diff,
1.9 KB
(added by randomandy, 4 years ago) |
|
|
-
|
a
|
b
|
|
| 182 | 182 | |
| 183 | 183 | We'll talk about the ``tg_errors`` argument later. First, let's have a look |
| 184 | 184 | how the form widget is used in the template. Here's the body contents of |
| 185 | | ``formstutorial/templates/index.kid``:: |
| | 185 | ``formstutorial/templates/add.kid``:: |
| 186 | 186 | |
| 187 | 187 | <p py:content="form.display(submit_text='Add Comment')">Comment form</p> |
| 188 | 188 | |
| … |
… |
|
| 296 | 296 | If you look at the definition of ``CommentFields`` repeated above, you'll see |
| 297 | 297 | that there is a validator for each of the first three fields. These validators |
| 298 | 298 | are part of the ``turbogears.validators`` package, which is a thin wrapper |
| 299 | | around Ian Bicking's `FormEncode`_ project. Since all values in a form are |
| 300 | | sent as strings, validators both convert the value to the appropriate Python |
| 301 | | type and check that the value matches a criteria in one step because one |
| 302 | | usually requires the other. For example, if your validator requires a numeric |
| 303 | | input be greater than 5 and you get ``"10"``, you have to convert ``"10"`` to |
| 304 | | the int ``10`` before a meaningful comparison can be made. In this case, we're |
| 305 | | not doing type conversion for any of our fields, but it's a useful thing to |
| 306 | | know. |
| | 299 | around Ian Bicking's `FormEncode`_ project. Since all values in a form are sent |
| | 300 | as strings, validators will both, |
| | 301 | |
| | 302 | #. convert the value to the appropriate Python type, and ... |
| | 303 | #. check that the value matches your designated criteria. |
| | 304 | |
| | 305 | Validators perform the two steps together because the value criteria frequently |
| | 306 | mean little until type conversion has taken place, or vice versa. In our |
| | 307 | tutorial, no type conversions were needed. But if, for example, your validator |
| | 308 | requires a numeric input be greater than 5 but your form input was ``"10"``, |
| | 309 | you have to convert ``"10"`` to the int ``10`` before a meaningful comparison |
| | 310 | can be made. |
| 307 | 311 | |
| 308 | 312 | .. _FormEncode: http://www.formencode.org |
| 309 | 313 | |
Download in other formats: