atom feed3 messages in com.googlegroups.pylons-discussRe: Dynamic Schema generation for For...
FromSent OnAttachments
Frederik ElwertOct 27, 2007 10:51 am 
Ian BickingOct 27, 2007 11:16 am 
FrederikOct 28, 2007 3:25 am 
Subject:Re: Dynamic Schema generation for FormEncode
From:Ian Bicking (
Date:Oct 27, 2007 11:16:44 am

Frederik Elwert wrote:

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?

cust_schema = formencode.Schema(allow_extra_fields=True, filter_extra_fields=True) cust_schema.add_field('v1', formencode.validators.Email(not_empty=True) cust_schema.add_field('v2', formencode.validators.Int(not_empty=False, max=100)

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')


Somehow I thought they were there generally, but they weren't. In the FormEncode trunk they work.