Discussion:
[FE-discuss] Using Any compound validation
Richie Ward
2009-07-12 19:20:04 UTC
Permalink
I was trying to do something like this for my validation:
chained_validators = [Any(NotEmpty('modulenamelist'), NotEmpty('modulename'))]

But NotEmpty does not support specifying the field names?

I am trying to make something like Field A or Field B must have some
valid input in it but both must not be empty. One can be empty if the
other has valid input.

I found the Any compound validator and this seemed to be what I need
but as you can see I hit a brick wall.
--
Thanks, Richie Ward
Andrea Riciputi
2009-07-13 07:22:41 UTC
Permalink
Hi Richie,
I had a similar problem some time ago and solved it subclassing
FormValidator() and adding my own validator to the list of the chained
validators. Something like the following:

class ThisOrThat(FormValidator):
messages = {
'thisorthat': u"This or that must have a valid input"
}

def validate_python(self, values, state):
this = values('this').strip()
that = values('that').strip()

if not (this or that):
raise Invalid(
self.message('thisorthat', state),
values,
state,
error_dict={'this': Invalid(
self.message('thisorthat', state),
values, state),
'that': Invalid(
self.message('thisorthat', state),
values, state)
})

I don't know if this is the best approach or not, I'm still a beginner
with FormEncode. Anyway I hope this can help you a bit.

Cheers,
Andrea
Post by Richie Ward
chained_validators = [Any(NotEmpty('modulenamelist'),
NotEmpty('modulename'))]
But NotEmpty does not support specifying the field names?
I am trying to make something like Field A or Field B must have some
valid input in it but both must not be empty. One can be empty if the
other has valid input.
I found the Any compound validator and this seemed to be what I need
but as you can see I hit a brick wall.
--
Thanks, Richie Ward
Loading...