Warning: Can't synchronize with repository "(default)" (Unsupported version control system "svn": No module named svn). Look in the Trac log for more information.

Ticket #1767: SimpleWidgetForm.diff

File SimpleWidgetForm.diff, 1.9 KB (added by randomandy, 4 years ago)
  • (a) SimpleWidgetForm vs. (b) SimpleWidgetForm_update

    a b  
    182182 
    183183We'll talk about the ``tg_errors`` argument later. First, let's have a look 
    184184how the form widget is used in the template. Here's the body contents of 
    185 ``formstutorial/templates/index.kid``:: 
     185``formstutorial/templates/add.kid``:: 
    186186 
    187187    <p py:content="form.display(submit_text='Add Comment')">Comment form</p> 
    188188 
     
    296296If you look at the definition of ``CommentFields`` repeated above, you'll see 
    297297that there is a validator for each of the first three fields. These validators 
    298298are 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. 
     299around Ian Bicking's `FormEncode`_ project. Since all values in a form are sent 
     300as 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 
     305Validators perform the two steps together because the value criteria frequently 
     306mean little until type conversion has taken place, or vice versa. In our 
     307tutorial, no type conversions were needed. But if, for example, your validator 
     308requires a numeric input be greater than 5 but your form input was ``"10"``, 
     309you have to convert ``"10"`` to the int ``10`` before a meaningful comparison 
     310can be made.  
    307311 
    308312.. _FormEncode: http://www.formencode.org 
    309313