Require min length only if field is present

Christopher Arndt <chris.arndt-S0/[email protected]>
Newsgroups gmane.comp.python.formencode
Message-ID <[email protected]>
Hi,

I have a seemingly simple problem, but I can't quite get my head around it:

I have a user account editing form, where the user should be able to change the
password if he enters the old one and the new one plus confirmation. The form
is validated through a schema (via the turbogears @validate decorator). This is
the schema:

class ExistingUserSchema(validators.Schema):
    email = validators.All(
        validators.Email(not_empty=True, max=255),
        UniqueEmail)
    old_password = validators.All(
        validators.UnicodeString(max=40),
        ValidPassword)
    password1 = validators.UnicodeString(min=passwd_minlen, minmax=40)
    password2 = validators.UnicodeString()
    chained_validators = [validators.All(
        validators.FieldsMatch('password1', 'password2'),
        validators.RequireIfPresent(required='old_password',
	  present='password1')
    )]

This doesn't work as expected, since the 'min' argument to the 'password1'
validator effectively requires this field. How can I require a field to have a
certain minimum length *only* if it is not empty? Can I accomplish this with
existing validators or do I have to write a custom one (which is the workaround
I'm using now)?

Or more generally, is there a validator, that applies another validator only if
a field is not empty?

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.