Re: Using Any compound validation
Andrea Riciputi <[email protected]> Mon, 13 Jul 2009 09:22:41 +0200
| Newsgroups | gmane.comp.python.formencode |
|---|---|
| Message-ID | <[email protected]> |
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
On 12 Jul, 2009, at 21:20, Richie Ward wrote:
> 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
------------------------------------------------------------------------------
Enter the BlackBerry Developer Challenge
This is your chance to win up to $100,000 in prizes! For a limited time,
vendors submitting new applications to BlackBerry App World(TM) will have
the opportunity to enter the BlackBerry Developer Challenge. See full prize
details at: http://p.sf.net/sfu/Challenge