Saving incomplete form with required fields (to prevent dynamically added form inputs from disappear

146 views
Skip to first unread message

Mark Li

unread,
Nov 23, 2013, 9:50:19 PM11/23/13
to web...@googlegroups.com

I currently have 2 tables as follows, with a form containing values both tables

db.define_table('playlist',
     
Field('title', notnull=True),
     
Field('description'),
     
Field('tags'),
     
Field('genre', 'reference genre')
     
Field('finished', 'boolean')
)

db
.define_table('playlist_tracks',
   
Field('playlist', 'reference playlist'),
   
Field('songname')
)



The form to add playlist_tracks is dynamic; a user enters a songname, which appends an input element with value=songname. When the user submits the form, if the form does not pass validation, then the playlist_tracks will all disappear when the page reloads, since they were added dynamically.

I want to prevent this from happening, as it can be very time consuming to add all the playlist tracks. I want to save the playlist_tracks in the reference table so they don't disappear, but it's not possible because there is no 'playlist' reference (the playlist didn't pass validation). I also want to give the user the option of "saving" an incomplete form to work on later, which has the same problem because the incomplete form might not pass validation, and thus there is no 'playlist' reference.

What would be the best method of saving this incomplete form, so the dynamically added playlist tracks will have a playlist reference?

I was thinking about inserting the record with dummy default values, after validation fails, but that has the disadvantage of automatically filling a required field that the user didn't choose. For example, if a user doesn't enter a title and doesn't select a genre from the select drop-down, then after validation fails, I'll insert the record with "Untitled Playlist" and genre==1 (and all other fields that passed validation would remain unchanged) but still show the form errors.


Mark Li

unread,
Nov 24, 2013, 2:44:05 AM11/24/13
to web...@googlegroups.com
I think I've found an optimal solution for this.

Since the validators for my table are only enforced at the form-level, I first insert a blank playlists record, and then use a SQLFORM or CRUD to update the newly inserted record. I can't submit an incomplete form, but I can insert an incomplete record and mark the "finished" column as false.

This way, the playlist id already exists and I can save the playlist_tracks to the reference table. The validators will still apply when the user submits the form.

Dave S

unread,
Nov 25, 2013, 2:15:14 PM11/25/13
to web...@googlegroups.com
On Saturday, November 23, 2013 11:44:05 PM UTC-8, Mark Li wrote:
I think I've found an optimal solution for this.

Since the validators for my table are only enforced at the form-level, I first insert a blank playlists record, and then use a SQLFORM or CRUD to update the newly inserted record. I can't submit an incomplete form, but I can insert an incomplete record and mark the "finished" column as false.

This way, the playlist id already exists and I can save the playlist_tracks to the reference table. The validators will still apply when the user submits the form.


As long as the "finished" field is adequate protection from having an incomplete or invalid entry messing up some of your db table processing at a later time if the user doesn't correct the form.  I think I would have done something with storing partials in the session, but maybe one the Respected Regulars has some alternative advice.

/dps

Mark Li

unread,
Nov 27, 2013, 1:19:12 AM11/27/13
to web...@googlegroups.com
Yep, the "finished" field will prevent the unfinished records from being exposed where they shouldn't be.



--
Resources:
- http://web2py.com
- http://web2py.com/book (Documentation)
- http://github.com/web2py/web2py (Source code)
- https://code.google.com/p/web2py/issues/list (Report Issues)
---
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/GG0oaBNSthE/unsubscribe.
To unsubscribe from this group and all its topics, send an email to web2py+un...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Reply all
Reply to author
Forward
0 new messages