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
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, ...
> You may be interrested by this :
>
>
> http://web2py.com/book/default/chapter/07?search=SQLFORM.factory#One-form-for-multiple-tables
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..
Is there some development about that solution for sub-form?