Hi everyone,
I believe the question here is, "how do I inject error messages into a
Deform-rendered form from outside the Deform/Colander APIs?" I run into
this most often when trying to gracefully handle errors (such as database
integrity errors) that are thrown after I've validated the form.
It's actually straightforward to inject errors, but I couldn't find
anything in the docs. I'm including a brief self-contained example below
(untested), but also see the github gist at the bottom for a more complete
version that I'm using in development:
import colander
import peppercorn
from deform import schema
from deform.form import Form
from deform.exception import ValidationFailure
# handy response type for illustrative purposes
class MaybeSuccess(object):
def __init__(self, succeeded, field=None, error=None):
self.succeeded = succeeded
self.field = field
self.error = error
schema = MySchema()
form = Form(schema)
if 'submit' in request.POST:
controls = request.POST.items()
try:
deserialized = form.validate(controls)
# do_something returns a MaybeSuccess object
reply = do_something(deserialized)
if reply.succeeded:
return HTTPFound(location='http://example/success')
else:
schemanode = form.schema[reply.field]
# manually create a colander.Invalid exception
exc = colander.Invalid(schemanode, reply.error)
# attach the exception object to the form node
form[reply.field].error = exc
# recreate the cstruct from the controls
cstruct = form.deserialize(peppercorn.parse(controls))
raise ValidationFailure(form, cstruct, None)
except ValidationFailure, e:
return {'form':e.render()}
else:
return {'form':form.render()}
When you raise and catch the ValidationFailure in that example, the form
that it renders will include the error message (reply.error) on the
appropriate node/field (reply.field).
I'm working on a form-heavy application where this and a few other things
came up frequently, so anyone interested should check out the small module
in this gist: https://gist.github.com/1775626
It provides a standardized response type that your post-validation
processing can use to indicate success (optionally returning an object) or
failure (including one or more error messages and the nodes they should be
attached to). I'm using it with SQLAlchemy right now and it's cut down on
the code in my views considerably. I can add documentation and make it into
an actual package if there's any interest.
Thanks!
Eric
On Mon, Feb 6, 2012 at 9:31 AM, Dirley <[email protected]> wrote:
> Excuse me if I'm missing something (I don't know formencode and also didn't
> read all your code), but couldn't you declare a callback within your view
> and
> set it as a validator of the field? Thus the validator could carry the
> "context" of the view. The colander.Function validator could be used for
> this:
>
> def login(self):
> schema = FormSchema()
>
> def is_email_registered(value):
> return model.useraccount.get_by_emailAddress(value)
>
> schema['email_address'].validator =
> colander.Function(is_email_registered,
> message='This email is not
> registered')
>
> # ... deform form boilerplate
>
>
> - D
>
>
>
> 2012/2/4 Jonathan Vanasco <[email protected]>
>
>> Well... I could probably do these validations within the validators, I
>> just don't want to.
>>
>> I've got the gist ( https://gist.github.com/1734244 ) working pretty
>> much perfectly to handle my needs right now. I dropped all the
>> 'validators' handling, and am just limiting it to schemas -- because
>> its simpler and i don't need otherwise.
>>
>> i'll package it up into something and toss it on pypi later.
>>
>> --
>> 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.
>>
>>
> --
> 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.
>
--
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.