Re: Why does Email(not_empty=True) accept empty values?

Ian Bicking <[email protected]> Mon, 11 Aug 2008 14:05:00 -0500
Newsgroups gmane.comp.python.formencode
Message-ID <[email protected]>
Gustavo Narea wrote:
> Hello, everyone.
> 
> I don't understand why I get this:
>>>>> validator = EmailValidator()
>>>>> print validator.to_python('')
>> None
> 
> being EmailValidator defined as:
> ====-start-=====
> class EmailValidator(FancyValidator):
>     
>     def __init__(self, not_empty=True, resolve_domain=False, max=255,
>                  **kwargs):
>         super(EmailValidator, self).__init__(**kwargs)
>         self.length_validator = MaxLength(max)
>         self.email_validator = Email(not_empty=not_empty, strip=True,
>                                      resolve_domain=resolve_domain)
>     
>     def validate_python(self, value, state):
>         try:
>             self.length_validator.to_python(value, state)
>             return self.email_validator.to_python(value, state)
>         except Invalid, msg:
>             raise InvalidEmailError(msg, value, state)
> =====-end-=====
> 
> Shouldn't validate_python() raise an exception if it receives an empty string?
> 
> Thanks in advance.

All validators (at least the ones that subclass FancyValidator) accept 
empty unless you say not_empty=True -- the standard validate_python 
routing isn't even called in the case of an empty value.

They used to actually be a mix, where some would accept empty and others 
would not, but this caused confusion for a lot of people.

-- 
Ian Bicking : [email protected] : http://blog.ianbicking.org

-------------------------------------------------------------------------
This SF.Net email is sponsored by the Moblin Your Move Developer's challenge
Build the coolest Linux based applications with Moblin SDK & win great prizes
Grand prize is a trip for two to an Open Source event anywhere in the world
http://moblin-contest.org/redirect.php?banner_id=100&url=/