Graham, thank you for the example.
I'll need to look into the pylons source to understand what you're doing.
FYI - here is what I've done
#Schema.py
class SomeForm(formencode.Schema):
##This works with both dict(request.params) and request.params.mixed(),
##but it will give the default message, and not the alternative I've
specified
sm = formencode.validators.NotEmpty(messages={'empty':'You must
choose at least one value.'})
##If I change request.params to request.params.mixed(), the sm
variable will be a list
##as expected, NotEmpty, however doing something like Int() throws
errors on
##the String values that are in the sm list within request.params,
so at least I can
##get it to do something
#sm = formencode.ForEach(formencode.validators.NotEmpty(),
formencode.validators.Int())
#controller
def submit_some_form(self):
if request.params:
try:
results = model.forms.schema.SomeForm.to_python(
dict(request.params),
state=c
)
return Response('Success!')
except formencode.Invalid, e:
c.form = model.forms.build.StandardForm(
#for a list
request.params.mixed()
#dict(request.params),
e.error_dict or {}
)
return render_response('some-form.html')
#some-form.html (mako template)
${c.form.start(name='form', id='form',
action=h.url_for(action='submit_some_form'), method="POST")}
${c.form.get_error('sm')
${c.form.field.checkbox('sm', value='ground')Ground
${c.form.field.checkbox('sm', value='expedited')Expedited
${c.form.field.checkbox('sm', value='next_day')Next Day
${c.form.field.submit('submit', value="Submit")}
${c.form.end()}
Graham Stratton wrote:
> 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
-~----------~----~----~----~------~----~------~--~---