#6092: URL and email fields need to allow custom validators
-------------------------------------+--------------------------------------
          Reporter:  jacob           |         Owner:  floguy
            Status:  assigned        |     Milestone:        
         Component:  Core framework  |       Version:  SVN   
        Resolution:                  |      Keywords:        
             Stage:  Accepted        |     Has_patch:  1     
        Needs_docs:  0               |   Needs_tests:  0     
Needs_better_patch:  1               |  
-------------------------------------+--------------------------------------
Comment (by mlavin):

 This ticket hasn't seen much attention lately and I question how relevant
 this ticket is given the changes in the 1.2 release allowing pluggable
 validators. [http://docs.djangoproject.com/en/1.2/ref/validators/]

 Both the default email and url validation logic can be overridden with a
 sub-class of the appropriate field which I would argue as more DRY than a
 new kwarg. An example of the email field overrides (without the actual
 regex) is given below:

 {{{
 import re
 from django import forms
 from django.core.validators EmailValidator
 from django.db import models
 from django.utils.translation import ugettext_lazy as _

 rfc2822pattern = re.compile(...)
 validate_email = EmailValidator(rfc2822pattern, _(u'Enter a valid e-mail
 address.'), 'invalid')

 class RFC2822EmailFormField(forms.EmailField)
     default_validators = [validate_email]


 class RFC2822EmailField(models.EmailField)
     default_validators = [validate_email]

     def formfield(self, **kwargs):
         defaults = {
             'form_class': RFC2822EmailFormField,
         }
         defaults.update(kwargs)
         return super(RFC2822EmailField, self).formfield(**defaults)

 }}}

-- 
Ticket URL: <http://code.djangoproject.com/ticket/6092#comment:9>
Django <http://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/django-updates?hl=en.

Reply via email to