Several controllers inserting and updating one row via forms

45 views
Skip to first unread message

shapova...@gmail.com

unread,
Aug 29, 2013, 9:14:52 AM8/29/13
to
Hi!

I have an issue with UPDATE form. I'm trying to implement something like enrollment form that has man field beyond standard name, email, etc. For the sake of usability I've decided to split it to two steps (i.e. pages): first asks user for name and email, and after user submits it, he/she gets redirected to the second controller with UPDATE form for the same record, inserted with previous form.

Btw, at first I thought of having one-page form and presenting it in parts with help of JS, but I've decided to go with back end, to have even partially submitted data in database. 

def firstform():
    form
= SQLFORM(db.person, fields = ['email','name'])
   
if form.process(session=None, formname='begin_form').accepted:        
        session
.person_id = form.vars.id
        session
.flash = 'form accepted'
        redirect
(URL('endjoin'))
   
elif form.errors:
        response
.flash = 'form has errors'
   
else:
        response
.flash = 'please fill the form'    
   
return dict()

# and
def secondform():    
    record
= db.person(session.person_id) or redirect(URL('index'))
    form
= SQLFORM(db.person, record, fields = ['address','phone','many_more_fields'])
   
if form.process(session=None, formname='end_form').accepted:
        response
.flash = 'form accepted'        
   
elif form.errors:
        response
.flash = 'form has errors'
   
else:
        response
.flash = 'please fill the form'
   
return dict()

The problems is that second action 'firstform' makes insert, instead of update to the record inserted by first action 'secondform'. Though 'record' contains reference to last add row, which should be updated(completed). I'm not passing form to the view, I use custom html to present my forms, because I didn't find a way to make helpers specify input type='email', placeholder, size and other HTML5 form parts attributes.

Vinicius Assef

unread,
Aug 29, 2013, 9:32:52 AM8/29/13
to web2py
It would be nice if you show your view.

Probably you're missing a hidden field in second form with the record id.

On Thu, Aug 29, 2013 at 10:12 AM, <shapova...@gmail.com> wrote:
> Hi!
>
> I have an issue with UPDATE form. I'm trying to implement something like
> enrollment form that has man field beyond standard name, email, etc. For the
> sake of usability I've decided to split it to two steps (i.e. controllers -
> pages): first asks user for name and email, and after user submits it,
> he/she gets redirected to the second controller with UPDATE form for the
> same record, inserted with previous form.
>
> Btw, at first I thought of having a one page form and presenting it in parts
> --
>
> ---
> You received this message because you are subscribed to the Google Groups
> "web2py-users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to web2py+un...@googlegroups.com.
> For more options, visit https://groups.google.com/groups/opt_out.

shapova...@gmail.com

unread,
Aug 30, 2013, 4:27:06 AM8/30/13
to web...@googlegroups.com
Thanks, I indeed missed that hidden field with record id. From the book: "Edit forms also contain a hidden INPUT field with name="id" which is used to identify the record. "

Though I don't really understand why we need that field as record id is kept server-side anyway.

Vinicius Assef

unread,
Aug 30, 2013, 8:43:03 AM8/30/13
to web2py
Because SQLFORM uses this field to know you're editing not inserting a record.

Denis Sh

unread,
Aug 30, 2013, 9:43:44 AM8/30/13
to web...@googlegroups.com
Hm, I thought that form = SQLFORM(db.person, record) , i.e. parameter 'record' indicates that, so why duplicate that in view? 


You received this message because you are subscribed to a topic in the Google Groups "web2py-users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/web2py/ns0C3Upvd_c/unsubscribe.
To unsubscribe from this group and all its topics, send an email to web2py+un...@googlegroups.com.

Vinicius Assef

unread,
Aug 30, 2013, 9:57:11 AM8/30/13
to web2py
Because if the hidden id field is in your request.post_vars, the
record's new data was submitted.
Reply all
Reply to author
Forward
0 new messages