3 messages in com.googlegroups.pylons-discussDynamic Schema generation for FormEncode
FromSent OnAttachments
Frederik Elwert27 Oct 2007 10:51 
Ian Bicking27 Oct 2007 11:16 
Frederik28 Oct 2007 03:24 
Subject:Dynamic Schema generation for FormEncode
From:Frederik Elwert (fred@public.gmane.org)
Date:10/27/2007 10:51:31 AM
List:com.googlegroups.pylons-discuss

Hi!

I am currently writing a web survey application where users can generate their own surveys.

I have the form generation in place, so forms are generated from the user's survey definition file. Now I want to add validation. As far as I understood the concept of FormEncode, it expects me to know what kind of values I expect when setting up the Schema.

E.g., from the docs:

class EmailForm(formencode.Schema): allow_extra_fields = True filter_extra_fields = True email = formencode.validators.Email(not_empty=True)

Now I can't "hardcode" that I have a variable of type email, I just have information about the variables like:

[('v1', {'type': 'email', 'required': True}), ('v2', {'type: 'int', 'max': 100})]

Now I want to set up a Schema from this information, equivalent to this:

class SurveyForm(formencode.Schema): allow_extra_fields = True filter_extra_fields = True v1 = formencode.validators.Email(not_empty=True) v2 = formencode.validators.Int(not_empty=False, max=100)

Is it somehow possible to generate Schema definitions dynamically like this?

By the way, Ian suggested that there were min and max keyword arguments for the Int validator in the "FormEncode form validators" thread, but I don't get them to work:

val = validators.Int(max=5) val.to_python('10')

10

Thanks, Frederik