Ticket #4861: 4861-regexfield.3.patch
File 4861-regexfield.3.patch, 4.0 KB (added by , 17 years ago) |
---|
-
django/db/models/fields/__init__.py
657 657 validators.isValidEmail(field_data, all_data) 658 658 659 659 def formfield(self, **kwargs): 660 defaults = {'form_class': forms.EmailField }660 defaults = {'form_class': forms.EmailField, 'max_length': self.maxlength} 661 661 defaults.update(kwargs) 662 662 return super(EmailField, self).formfield(**defaults) 663 663 … … 950 950 return "CharField" 951 951 952 952 def formfield(self, **kwargs): 953 defaults = {'form_class': forms.URLField, 'verify_exists': self.verify_exists} 953 defaults = { 954 'form_class': forms.URLField, 955 'verify_exists': self.verify_exists, 956 'max_length': self.maxlength 957 } 954 958 defaults.update(kwargs) 955 959 return super(URLField, self).formfield(**defaults) 956 960 -
django/newforms/fields.py
295 295 continue 296 296 raise ValidationError(ugettext(u'Enter a valid date/time.')) 297 297 298 class RegexField( Field):299 def __init__(self, regex, max_length=None, min_length=None,error_message=None, *args, **kwargs):298 class RegexField(CharField): 299 def __init__(self, regex, error_message=None, *args, **kwargs): 300 300 """ 301 301 regex can be either a string or a compiled regular expression object. 302 302 error_message is an optional error message to use, if … … 306 306 if isinstance(regex, basestring): 307 307 regex = re.compile(regex) 308 308 self.regex = regex 309 self.max_length, self.min_length = max_length, min_length310 309 self.error_message = error_message or ugettext(u'Enter a valid value.') 311 310 312 311 def clean(self, value): … … 314 313 Validates that the input matches the regular expression. Returns a 315 314 Unicode object. 316 315 """ 317 super(RegexField, self).clean(value) 318 if value in EMPTY_VALUES: 319 value = u'' 320 value = smart_unicode(value) 316 value = super(RegexField, self).clean(value) 321 317 if value == u'': 322 318 return value 323 if self.max_length is not None and len(value) > self.max_length:324 raise ValidationError(ugettext(u'Ensure this value has at most %d characters.') % self.max_length)325 if self.min_length is not None and len(value) < self.min_length:326 raise ValidationError(ugettext(u'Ensure this value has at least %d characters.') % self.min_length)327 319 if not self.regex.search(value): 328 320 raise ValidationError(self.error_message) 329 321 return value … … 334 326 r')@(?:[A-Z0-9-]+\.)+[A-Z]{2,6}$', re.IGNORECASE) # domain 335 327 336 328 class EmailField(RegexField): 337 def __init__(self, max_length=None, min_length=None,*args, **kwargs):338 RegexField.__init__(self, email_re, max_length, min_length,339 ugettext(u'Enter a valid e-mail address.'),*args, **kwargs)329 def __init__(self, *args, **kwargs): 330 super(EmailField, self).__init__(self, email_re, ugettext(u'Enter a valid e-mail address.'), 331 *args, **kwargs) 340 332 341 333 url_re = re.compile( 342 334 r'^https?://' # http:// or https:// … … 352 344 URL_VALIDATOR_USER_AGENT = 'Django (http://www.djangoproject.com/)' 353 345 354 346 class URLField(RegexField): 355 def __init__(self, max_length=None, min_length=None,verify_exists=False,347 def __init__(self, verify_exists=False, 356 348 validator_user_agent=URL_VALIDATOR_USER_AGENT, *args, **kwargs): 357 super(URLField, self).__init__(url_re, max_length, min_length,ugettext(u'Enter a valid URL.'), *args, **kwargs)349 super(URLField, self).__init__(url_re, ugettext(u'Enter a valid URL.'), *args, **kwargs) 358 350 self.verify_exists = verify_exists 359 351 self.user_agent = validator_user_agent 360 352