I should also note that while this method has the advantage of using 
web2py's built-in data-verifiers, if you use FormEncode, you can get a 
bit more power and flexibility for about the same amount of code.  
Verifying the form I posted with FormEncode would look like:

import formencode

class FormValidator(formencode.Schema):
    name = validators.Regex(r'[A-Za-z0-9-\.\']{4,40}')
    county = validators.OneOf(COUNTIES)
    td = validators.Regex(r'on', if_missing='', if_empty='')

try:
    data = FormValidator(allow_extra_fields = 
True).to_python(dict(request.vars))
    # Validation succeeded, continue
except formencode.Invalid, e:
    # Validation failed, see messages in e
    message = e.message

Bummer (for FormEncode) that form.accepts is so much cleaner than the 
FormEncode version.   Maybe I'll wrap it into a nice pretty package.

-tim

Timothy Farrell wrote:
> Bill,
>
> I crossed this bridge a few weeks ago.  I, like you, don't dig the whole 
> presentation logic in the controller.  However, it is still reasonable 
> to build a "form" in the controller for logical data-handling purposes.  
> Here's what I did:
>
> # Build our form
> form=FORM(INPUT(_type="text", _name="name"
>                  , requires=IS_MATCH('[A-Za-z0-9-\.\']{4,40}'))
>           ,SELECT(_name="county"
>                  ,requires=IS_IN_SET(COUNTIES) )
>           ,INPUT(_type="checkbox", _name="td"
>                  ,requires=IS_NULL_OR(IS_MATCH('on')))
>           )
> # If the submit validates, act accordingly
> if form.accepts(request.vars, formname="polinfo"):
>     # process passed data here...
>
> Once I made "form", I never passed it to the view, instead I used this form:
>
> <form method="POST"
>       id="polinfo"
>       action="">
>     <div>
>         <label for="name">Name:</label>
>         <input type="text"
>                name="name"
>                id="name" />
>     </div>
>     <div>
>         <label for="county">County:</label>
>         <select name="county"
>                 id="county">
>             <option></option>
>             <option value="Oklahoma">
>                 Oklahoma
>             </option>
>             <option value="Other">
>                 Other
>             </option>
>             <option value="Tulsa">
>                 Tulsa
>             </option>
>         </select>
>     </div>
>     <div>
>         <label for="td">TD:</label>
>         <input type="checkbox"
>                id="TD"
>                name="td" />
>         <input type="hidden"
>                name="_formname" value="polinfo" />
>     </div>
> </form>
>
> Notice that I included a hidden field "_formname".  This is the trick 
> that you need to get it to work.
>
> There you go, a happy marriage of both the ease of web2py forms while 
> maintaining proper MVC separation.
>
> -tim
>
> billf wrote:
>   
>> I am trying to get to grips with forms as part of an attempt to re-
>> engineer an existing Java webapp using web2py.
>>
>> I like the ability to generate a form automatically.  I especially
>> like the benefit that accepts() gives.  However, I can foresee many
>> situations where the auto-generated form is not what I want in terms
>> of layout.  I have looked a little at widgets but I don't think that
>> allows the ability to manage the overall form layout.  I know that I
>> can use the HTML building objects but I don't want presentation logic
>> in the controller.
>>
>> It seems to me (at the moment) that the ideal would be the ability,
>> when necessary, to design a form manually in a view using html/css but
>> be able to utilise the form.accepts() logic in the controller.
>>
>> I'm tempted to think that this is already possible.  If I code:
>>
>> form=SQLFORM(db.recipe)
>> if form.accepts(request.vars,session):
>>   whatever...
>>
>> ...I am assuming that this creates a form based on db.recipe and
>> populates the form.vars with request.vars and does the rest
>> (validation, updating db).  I just need to ensure that the
>> request.vars names are those expected by the form?
>>
>> Before I spend a lot of time on this am I heading up a dead-end?
>>
>> Bill
>>
>>
>>     
>>   
>>     
>
> >
>   

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"web2py Web Framework" group.
To post to this group, send email to web2py@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/web2py?hl=en
-~----------~----~----~----~------~----~------~--~---

begin:vcard
fn:Timothy Farrell
n:Farrell;Timothy
org:Statewide General Insurance Agency;IT
adr:;;4501 East 31st Street;Tulsa;OK;74135;US
email;internet:[EMAIL PROTECTED]
title:Computer Guy
tel;work:(918)492-1446
url:www.swgen.com
version:2.1
end:vcard

Reply via email to