The validation "state" can be set using the "validation_ctx" method in
the FormHandler class. Here's the new handler and decorator:
from pylons.controllers.util import redirect_to
from pylons.decorators import PylonsFormEncodeState
import decorator
import formencode
class FormHandler(object):
def check(self):
return None
def defaults(self):
return request.GET
def validation_ctx(self):
return None
def process(self, result):
redirect_to(**dict(request.GET))
def validate(schema, handler, **htmlfill_kwargs):
def wrapper(func, *args, **kwargs):
errors = handler.check()
if errors:
return errors
if request.method=='GET':
defaults = handler.defaults()
else:
vctx = handler.validation_ctx() or PylonsFormEncodeState
try:
result = schema.to_python(request.params.mixed(),
vctx)
except formencode.Invalid, e:
errors = e.unpack_errors()
if not errors:
return handler.process(result)
defaults = request.POST
#display form
content = func(*args, **kwargs)
return formencode.htmlfill.render(content, defaults=defaults,
errors=errors, **htmlfill_kwargs)
return decorator.decorator(wrapper)
On Jan 4, 4:48 am, "Mike Orr" <[email protected]> wrote:
> On Sun, Jan 4, 2009 at 3:57 AM, Tycon <[email protected]> wrote:
> > Using this decorator has greatly simplified my form handling code, so
> > if there is interest I can publish the source code for this decorator/
> > handler architecture.
>
> Yes, please publish it and put a link in ticket
> #405http://pylonshq.com/project/pylonshq/ticket/405
>
> There has been a proposal to split @validate into three parts that can
> be used independently in an action, while still keeping the decorator
> compatible. Then Mike Bayer proposed a second solution which uses
> Mako (and may be too much Mako code for a Pylons default?). But we
> are still looking for other approaches. I use @validate but am not
> that happy with it. I'm not sure that a validator is even the right
> solution to this problem, because you can't pass action-specific state
> through a decorator and that's often the most interesting state (the
> database record corresponding to the request). One problem with
> @validate currently is that it mixes code that's part of the basic
> pattern with code to make the decorator flexible, and that makes it
> cumbersome to inline "the equivalent code" in an action.
>
> --
> Mike Orr <[email protected]>
--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---