Discussion:
[FE-discuss] Chained validators again
Andrea Riciputi
2009-03-06 15:59:13 UTC
Permalink
Hi,
I'm still having some issues with chained validators. Despite what
mentioned in the documentation, chained_validators **doesn't** always
allow for multiple validators to fail.

In fact the example in the documentation works as expected, but the
following one doesn't:

class Foo(Schema):
filter_extra_fields = True
allow_extra_fields = True
a = String()
b = String()
c = String()
d = String()
chained_validators = [
RequireIfPresent('a', present='b'),
RequireIfPresent('c', present='d'),
]
pass

foo = Foo()

try:
foo.to_python({'a':'', 'b':'bb', 'c':'', 'd':'dd'})
except fe.Invalid, err:
print err.error_dict

{'a': Invalid(u'Please enter a value',)}

whilst I'd have expected two error messages since both 'a' and 'c'
fields are missing! Could you explain me why this happens, and how I
can get all the chained validators actually evaluated?

Cheers,
Andrea
Ian Bicking
2009-03-06 16:51:29 UTC
Permalink
Post by Andrea Riciputi
Hi,
I'm still having some issues with chained validators. Despite what
mentioned in the documentation, chained_validators **doesn't** always
allow for multiple validators to fail.
For the validators to keep getting called, each validator has to have
a method validate_partial, which raises Invalid if there's an
exception, but whose return value is ignored, and the validator has to
have an attribute vaildate_partial_form which is True. There's just a
check in Schema that tests if that's the case for each chained
validator (once there's an error), and skips the validators that don't
have that.
--
Ian Bicking | http://blog.ianbicking.org
Andrea Riciputi
2009-03-09 09:46:43 UTC
Permalink
Thanks Ian,
I was able to make it work as inteded. However I was wondering if it
was better to make those kind of chained validators to inherit from
FormValidator instead that from FancyValidator. Which is the real
purpose of FormValidators? The documentation is quite terse.

Cheers,
Andrea
Post by Ian Bicking
For the validators to keep getting called, each validator has to have
a method validate_partial, which raises Invalid if there's an
exception, but whose return value is ignored, and the validator has to
have an attribute vaildate_partial_form which is True. There's just a
check in Schema that tests if that's the case for each chained
validator (once there's an error), and skips the validators that don't
have that.
Ian Bicking
2009-03-09 16:36:00 UTC
Permalink
Post by Andrea Riciputi
Thanks Ian,
I was able to make it work as inteded. However I was wondering if it was
better to make those kind of chained validators to inherit from
FormValidator instead that from FancyValidator. Which is the real purpose of
FormValidators? The documentation is quite terse.
Yeah, FormValidator would be better. FormValidators just validate
dictionaries instead of single values, but there are also a couple of
other small differences (like the definition of "empty").
--
Ian Bicking | http://blog.ianbicking.org
Loading...