Custom validation error messages

101 views
Skip to first unread message

Anand

unread,
Apr 24, 2012, 1:57:16 PM4/24/12
to pylons-...@googlegroups.com
Hi,

I am using deform with pyramid. I want to customize the error message for validation failures. My schema field looks like

username = colander.SchemaNode(colander.String(), name="username",
        title="Username", oid="login-username",
        widget=TextInputWidget(size=40, css_class="input-xlarge"))

This is a 'require' field. The default validator provided by colander throws a Invalid exception with a message 'Required'. I want to customize this message and provide more meaningful message. Is there a way to change this error message? I could write a custom validator, but the 'required' field validation happens before my custom validator is executed. Besides, I don't want to write custom validators for every field in my application.

Is there a way to pass in a validation message to colander field validations?

Anand

Douglas Cerna

unread,
Apr 30, 2012, 1:41:49 PM4/30/12
to pylons-...@googlegroups.com
I do it this (very dumb and probably dangerous) way:

----- %< -----
class RequiredSchemaNode(colander.SchemaNode):

def deserialize(self, *args, **kw):
try:
return super(RequiredSchemaNode, self).deserialize(*args, **kw)
except (colander.Invalid,), e:
if e.args[-1] == 'Required':
raise colander.Invalid(self, _('Required input is missing.'))
raise
----- %< -----

I hope at least give you an idea.
> --
> You received this message because you are subscribed to the Google Groups
> "pylons-discuss" group.
> To view this discussion on the web visit
> https://groups.google.com/d/msg/pylons-discuss/-/uF71jHsbIv0J.
> To post to this group, send email to pylons-...@googlegroups.com.
> To unsubscribe from this group, send email to
> pylons-discus...@googlegroups.com.
> For more options, visit this group at
> http://groups.google.com/group/pylons-discuss?hl=en.
>
>
Reply all
Reply to author
Forward
0 new messages