Sometimes I need to define my forms in the view, but still want to use as 
much of web2py's form functionality as possible. In those cases I generally 
define the form in the controller as well, with corresponding fields, and 
then build a form in the view with the same formname.

This works, except that I can't figure out how to make use of formkey to 
prevent double posting in this scenario?

*Controller:*

def index():
 
    my_form = FORM(INPUT(_name='name'))
 
        if my_form.accepts(request.vars,formname='my_form'):
 
        logger.debug(my_form.vars.name)
 
        import time
        time.sleep(1) # Wait a little to allow a double submission
 
        response.flash = "Form accepted."
 
    elif my_form.errors:
        response.flash = str(my_form.errors)
    else:
        pass
 
    return dict()


*View:*

{{extend 'layout.html'}}

<form method="POST">
    <input type="text" name="name">
    <input type="submit" value="Double Submit Me">
    <input type="hidden" name="_formname" value="my_form">
</form>

-- 



Reply via email to