2 messages in com.googlegroups.pylons-discussfiltering input with formencode.Fancy...
FromSent OnAttachments
noflashlight29 Jun 2008 19:34 
rcs_comp29 Jun 2008 20:27 
Subject:filtering input with formencode.FancyValidator
From:noflashlight (Nick@gmail.com)
Date:06/29/2008 07:34:23 PM
List:com.googlegroups.pylons-discuss

I've been trying to write my own validators using the formencode.FancyValidator class. What I am trying to accomplish is to validate some input as well as modify the input by stripping html tags. I'm not sure if formencode was designed to accomplish both of these tasks.

Here is a simple example:

import formencode, HTMLParser from formencode import validators from HTMLParser import HTMLParser

class StripTags(formencode.FancyValidator): def _to_python(self, value, state): result = [] parser = HTMLParser() parser.handle_data = result.append parser.feed(value) parser.close() return ''.join(result)

class CommentForm(formencode.Schema): name = formencode.All(StripTags(), validators.NotEmpty()) comment = formencode.All(StripTags(), validators.NotEmpty())

So in this example I am trying to validate that the name & comment fields are not empty, as well as strip any html or script tags. Now I can raise an exception if any tags are encountered and that works fine, but I'd rather strip them out.

Am I going about this the wrong way? I'm starting to think that formencode is only designed for validating input, and not filtering it.