Re: Require min length only if field is present

Christopher Arndt <chris.arndt-S0/[email protected]>
Newsgroups gmane.comp.python.formencode
Message-ID <[email protected]>
Christopher Arndt schrieb:
> Or more generally, is there a validator, that applies another validator only if
> a field is not empty?

Sorry, to reply to myself, but I just found the solution:

    password1 = validators.Any(
        validators.UnicodeString(min=passwd_minlen, minmax=40, strip=True),
        validators.Empty
    )


I thought, I had tried this, but I must have accidentally used 'validators.All'
instead.

Please note that the order of the validators in validators.Any is important,
because if you put validators.Empty first and there is a value but the second
validator raises Invalid, you will get the error message from the Empty
validator, saying "You cannot enter a value here", which is probably not what
you want.

Chris

-------------------------------------------------------------------------
Take Surveys. Earn Cash. Influence the Future of IT
Join SourceForge.net's Techsay panel and you'll get the chance to share your
opinions on IT & business topics through brief surveys-and earn cash
http://www.techsay.com/default.php?page=join.php&p=sourceforge&CID=DEVDEV
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.