Formencode can handle it..
You need name your input fields to something like:
<input name="classlist_1.id"
<input name="classlist_1.name"
<input name="classlist_1.registration.....
<input name="classlist_2.id
<input name="classlist_2.name
<input name="classlist_2.registration.....
And then, on the controller side:
class MyClassValidator(formencode.Schema):
id=Int()
name=String()...
class MyClassListValidator(formencode.Schema):
allow_extra_fields = True
filter_extra_fields = True
classlist = formencode.foreach.ForEach(MyClassValidator())
@validate(schema=MyClassListValidator(), variable_decode=True, ...)
classlist = self.form_result.get('classlist')
#Here, classlist will be an array of objects.
Hope this helps.. It took a while to figure out, but is quite helpful
once it does.
DD.
On 12/22/2009 7:23 PM, Mike Orr wrote:
> On Tue, Dec 22, 2009 at 6:59 PM, Mark<[email protected]> wrote:
>> Hi guys,
>>
>> I have a class form that enables users to enter information about a
>> class (ie. class id, class type, course name, registration
>> deadline...) and also allows users to dynamically add sessions.
>> Sessions consists of start/end datetime, location and description. I
>> want them to add multiple sessions at a go before submitting the form
>> for processing and then saving the information to my database.
>>
>> If I have multiple session sub-forms in my class form, won't all of
>> them have the same name? Can I use name=session_description[]? How
>> would I capture the data in my controller?
>>
>> session_description[] = request.params.get('session_description')
>> for session in session_description:
>> Save to DB
>
> FormEncode has support for this but I've never been able to figure it
> out exactly. You'd put all the fields on one form, and name them
> something like 'session-1.description;. The NestedVariables
> pre-validator will unpack them into a list of dicts, so you'd use
> something like ForEach('session'), with a schema validator for the
> session. But I've never quite figured it out so you'll have to read
> the FormEncode manual and the source. You can either put a fixed
> number of rows on the form, or have a Javascript button to add a row.
>
--
You received this message because you are subscribed to the Google Groups
"pylons-discuss" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/pylons-discuss?hl=en.