Hi Dan,

I've been struggling a bit with FormEncode too.  Here's some
controller code that almost works for me.  To make it work, I had to
change line 106 of pylons/decorators/__init__.py tp

response.content = [htmlfill.render(form_content, decoded, errors)]

(Passing 'decoded' instead of 'params', which haven't been through
variable_decode and therefore don't work with multiple values).  I'm
not sure whether this is a bug or whether I'm using the decorator
wrongly.

I also don't understand why my LengthOneValidator doesn't raise an
exception for zero items selected, but NotEmpty does.  I suspect it
will be obvious in the morning.

Sorry for the poor example.  I hope this helps a little.

Graham


from formencodedemo.lib.base import *
from formencode.schema import Schema
from formencode.foreach import ForEach
from formencode.compound import All
from formencode import validators
from formencode.api import Invalid

class LengthOneValidator(validators.FancyValidator):
    def validate_python(self, value, state):
        if len(value) != 1:
            raise Invalid("Please select exactly one thingy", value,
state)

class DemoSchema(Schema):
    n = All(ForEach(validators.Int()), LengthOneValidator(),
validators.NotEmpty())

class FormController(BaseController):
    def index(self):
        return Response("""
<form action="validate" method="post">
<input type="checkbox" name="n" value="1" />1
<input type="checkbox" name="n" value="2" />2
<input type="checkbox" name="n" value="3" />3
<input type="checkbox" name="n" value="4" />4
<input type="submit" />
</form>""")

    @validate(schema=DemoSchema, form='index', variable_decode=True)
    def validate(self):
        return Response(str(self.form_result))


--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---

Reply via email to