Discussion:
[FE-discuss] Chained validators and error messages
Andrea Riciputi
2009-03-04 09:57:40 UTC
Permalink
Hi,
I'm pretty new to Formencode, so please forgive my inexperience.

I'm trying to make htmlfill to put the error message raised by a
chained validator near a specific form field.

To get this I've written a custom validator where the messages dict is
defined like this:

messages = {'my_error_label': u"Error message goes here" }

then, if the conditions are not met I raise the Invalid exception like
this:

raise Invalid(self.message('my_error_label', state),
values, state,
error_dict={
'my_error_label': Invalid(self.message('field_name', state), values,
state)
})

Despite the fact this gives me the expected result, I'm not sure I'm
raising that exception in the correct way. I mean, I got the
impression that the Invalid instance could be called in easier way,
but I really can't see how. Any suggestion?

TIA,
Andrea
Ian Bicking
2009-03-05 03:48:38 UTC
Permalink
Post by Andrea Riciputi
I'm trying to make htmlfill to put the error message raised by a
chained validator near a specific form field.
To get this I've written a custom validator where the messages dict is
messages = {'my_error_label': u"Error message goes here" }
then, if the conditions are not met I raise the Invalid exception like
raise Invalid(self.message('my_error_label', state),
values, state,
error_dict={
'my_error_label': Invalid(self.message('field_name', state), values,
state)
})
Despite the fact this gives me the expected result, I'm not sure I'm
raising that exception in the correct way. I mean, I got the
impression that the Invalid instance could be called in easier way,
but I really can't see how. Any suggestion?
Yeah, that's right -- it's a little awkward, but usually this
particular kind of exception is only raised in Schema, so people don't
see the awkwardness as much ;)
--
Ian Bicking | http://blog.ianbicking.org
Andrea Riciputi
2009-03-05 13:03:17 UTC
Permalink
Since you wrote that this is something like a special case, whilst i'm
finding myself using it over and over again, I'm wondering if there is
a better/more standard way to put error message raised by a chained
validator near a specific form field.

Or, if the chained validator is the standard way to solve my problem.
Basically, what I'm trying to do is to check if a field fulfill a
certain constraint where the constraint varies with the value of
another field. I thought that checking each field per se (according
their own validation rules) and then checking them in combination
(using a chained validator) was a good approach, but I'm a beginner so
I might be wrong. Is there any more standar way to get this done?

TIA,
Andrea
Post by Ian Bicking
Post by Andrea Riciputi
I'm trying to make htmlfill to put the error message raised by a
chained validator near a specific form field.
To get this I've written a custom validator where the messages dict is
messages = {'my_error_label': u"Error message goes here" }
then, if the conditions are not met I raise the Invalid exception like
raise Invalid(self.message('my_error_label', state),
values, state,
error_dict={
'my_error_label': Invalid(self.message('field_name', state), values,
state)
})
Despite the fact this gives me the expected result, I'm not sure I'm
raising that exception in the correct way. I mean, I got the
impression that the Invalid instance could be called in easier way,
but I really can't see how. Any suggestion?
Yeah, that's right -- it's a little awkward, but usually this
particular kind of exception is only raised in Schema, so people don't
see the awkwardness as much ;)
--
Ian Bicking | http://blog.ianbicking.org
Loading...