Tim - thanks for all the info. As a Python newbie I have never heard
of FormEncode.

Given what I know at this point I think I prefer to follow up your
first solution.

The first thing I didn't understand was why the form.accepts() needed
the name of the form in your view?  This got me digging and html.py
and sqlhtml.py (great to see all the arguments available) shed a lot
of light but there are still a lot of blanks as to how the whole thing
works.

I have boiled all my questions down to: "Can someone please explain
the sequence of events embodied in the following?"

form=SQLFORM(db.recipe)
if form.accepts(request.vars,session):
  response.flash="Successful" (or something)
return dict(form=form)

My version with holes is:
form=SQLFORM(db.recipe) > sqlhtml.py to create form from db.recipe

form.accepts(request.vars,session) > sqlhtml.py then calls
FORM.accepts() in html.py; if return is true then goes on to update db
                                                    > html.py
FORM.accepts() implemented by DIV.accepts()

Hole: I can't work out what is happening in DIV.accepts().  I'm
assuming there is some "first time" check - "we haven't any
request.vars yet" - that maybe uses the session but I can't see where
anything is stored in the session or how it uses the form name.  Any
help greatly appreciated.

Hole: I assume that if form.accepts() returns false then it does not
necessarily mean that there are errors - it could just be that the
form hasn't been output yet.  Is that correct?

Apologies if the questions are a bit vague.

Bill

On Oct 8, 10:12 pm, Timothy Farrell <[EMAIL PROTECTED]> wrote:
> 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
>
>
>
>  tfarrell.vcf
> < 1KViewDownload
--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---

Reply via email to