SQLFORM.factory Field2 depends on Field1

94 views
Skip to first unread message

cory n

unread,
Apr 26, 2012, 1:54:26 PM4/26/12
to web...@googlegroups.com
Okay, I want to make the second field of a SQLFORM.factory depend on what is chosen in the first field...  something like this...

form = SQLFORM.factory(
        Field('PN', requires=IS_NOT_EMPTY()),
        Field('Segment', requires=IS_IN_SET(['Segment1', 'Segment2'])),
        Field('Family', requires=IS_IN_SET(getFamilies(form.vars.Segment))))
        ...

Of course I can't use form.vars.Segment until the form is submitted, so how can I accomplish this?

So that the field 'Family' takes the value of the field 'Segment', does a calculation and returns the appropriate list of families.

I'm super new to web programming, so I apologize in advance if this is a dumb question.




Massimo Di Pierro

unread,
Apr 27, 2012, 9:13:58 AM4/27/12
to web...@googlegroups.com
IS_IN_SET(getFamilies(form.vars.Segment))))

should be

IS_IN_SET(getFamilies(request.vars.Segment))))

but I would not set this validator in models, I would set it in controller function, where needed.

cory n

unread,
Apr 27, 2012, 11:00:44 AM4/27/12
to web...@googlegroups.com
This works, after I submit once, then my selection for FIELD1 appears in FIELD2.  I need it to operate before a submit though, so that once a "Segment" is selected, I can use that selection in the other dropdown boxes before a submit.

Thanks

Cory

villas

unread,
Apr 27, 2012, 11:24:46 AM4/27/12
to web...@googlegroups.com
You can do that on the client side using javascript.  The jQuery library is useful. 
Read the jQuery section of the book: http://www.web2py.com/books/default/chapter/29/11
And google for example on:  jquery cascading selects

Anthony

unread,
Apr 27, 2012, 2:44:58 PM4/27/12
to web...@googlegroups.com

cory n

unread,
Apr 28, 2012, 11:48:29 PM4/28/12
to web...@googlegroups.com
Yeah, I thought it would have to be a little complicated, I was hoping there was an easy way out.

Any how, I got it working and will post it as a slice next week.  Thank you gentlemen.

Cory
Reply all
Reply to author
Forward
0 new messages