Easy handling of subforms

310 views
Skip to first unread message

~redShadow~

unread,
Nov 16, 2011, 3:12:16 PM11/16/11
to web...@googlegroups.com
For some reason, I need to simultaneously update a bunch of records
together in the same form. Specifically, records come from different
tables but field names sometimes overlaps, this not allowing to simply
merge the standard CRUD SQLFORMs.

I like a feature in the Drupal Forms API that's specifically to solve
any sub-forming problem: tree placement of fields.

So, when you create a form like this:

group0
element0
element1
group1
element0
element1
element2
group2
element0
element1

then the form generator creates fields named like:

edit[group0][element0]
edit[group0][element1]
edit[group1][element0]
edit[group1][element1]
edit[group1][element2]
edit[group1][group2][element0]
edit[group1][group2][element1]

grouped inside some <fieldset>s.

When the form gets submitted, vars are split (also by taking advantage
of how php parses query strings recursively) in a structure like this:

{
'group0' : {
'element0' : '..some value..',
'element1' : '..some value..',
},
'group1' : {
'element0' : '..some value..',
'element1' : '..some value..',
'element2' : '..some value..',
'group2' : {
'element0' : '..some value..',
'element1' : '..some value..',
}
}
}

So, extracting trees of values becomes quite easy.

Now the question is: is there anything like this inside web2py?
I looked for it in the book and in the code, but didn't find anything..

--
Samuele ~redShadow~ Santi
----------------------------------------------------------------
redshadow[at]hackzine.org - redshadowhack[at]gmail.com

Blog: http://hackzine.org

GPG Key signature:
050D 3E9F 6E0B 44CE C008 D1FC 166C 3C7E EB26 4933
----------------------------------------------------------------
/me recommends:
Squadra Informatica - http://www.squadrainformatica.com
----------------------------------------------------------------
- Proud ThinkPad T-Series owner
- Registered Linux-User: #440008
* GENTOO User since 1199142000 (2008-01-01)
* former DEBIAN SID user
----------------------------------------------------------------
"Software is like sex: it's better when it's free!"
-- Linus Torvalds

signature.asc

Richard Vézina

unread,
Nov 16, 2011, 3:36:34 PM11/16/11
to web...@googlegroups.com
Man!

I think you got a nice solution for this exact problem...

I had tried with jQuery in the pass to manage this by duplicating the field that I want to bunch insert for... It was working but was breaking the workflow of form validation if I remember... Web2py has change a lot since then and I don't know if Massimo has not resolved this issue (confirmation needed!!).

But I think what you suggest is pretty neath approach and easy to implement...

Web2py guru??

I suspect that you want to make insertion in multiple table...

You may be interrested by this :




Richard

~redShadow~

unread,
Nov 17, 2011, 12:16:16 PM11/17/11
to web...@googlegroups.com
On Wed, 2011-11-16 at 15:36 -0500, Richard Vézina wrote:
> Man!
>
>
> I think you got a nice solution for this exact problem...
>
>
> I had tried with jQuery in the pass to manage this by duplicating the
> field that I want to bunch insert for... It was working but was
> breaking the workflow of form validation if I remember... Web2py has
> change a lot since then and I don't know if Massimo has not resolved
> this issue (confirmation needed!!).
>
>
> But I think what you suggest is pretty neath approach and easy to
> implement...
>
>
> Web2py guru??
>
>
> I suspect that you want to make insertion in multiple table...

Yep, but I also want to have some control over this.. since whether to
insert/update a record is determined by other form fields, the number of
tables is variable and possibly there are clashing field names, ...

Tried that, but «This only works when the tables don't have field names
in common.».


Anyways, I managed to find a way that seems to work, in order to merge
SQLFORMs for multiple tables, adding a "namespace" in front of field
name, and then stripping it before doing inserts.

See relevant parts from the controller source code here:
http://pastebin.com/V1xwcjAg

This of course could be extended to build a tree from ``form.vars``,
instead of just a ``dict``-of-``dict``s (maybe by exploding
``my[var][name]``-style subscripted vars), etc..

signature.asc

Richard Vézina

unread,
Nov 22, 2011, 1:24:30 PM11/22/11
to web...@googlegroups.com
I think we need Massimo opinion on this...

Richard

Richard Vézina

unread,
Dec 8, 2011, 1:07:20 PM12/8/11
to web...@googlegroups.com
Bump...

Is there some development about that solution for sub-form?

Richard

Alan Etkin

unread,
May 5, 2013, 4:38:48 PM5/5/13
to web...@googlegroups.com
Is there some development about that solution for sub-form?

A multi-page simple approach for linked parent-child record forms (applied to invoices):
http://www.web2pyslices.com/slice/show/1627/simple-online-invoices-with-invoice2py
Reply all
Reply to author
Forward
0 new messages