From 47da7b7a9aefce66fa2c54833b5ba308781fc95e Mon Sep 17 00:00:00 2001 From: Claude Paroz Date: Sat, 30 Jun 2012 13:18:07 +0200 Subject: [PATCH] Fixed #18102 -- Defined min/max_length on French localflavor form fields Thanks mothsART for the report and the initial patch. --- django/contrib/localflavor/fr/forms.py | 11 +++++++---- tests/regressiontests/localflavor/fr/tests.py | 3 ++- 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/django/contrib/localflavor/fr/forms.py b/django/contrib/localflavor/fr/forms.py index 47177db685f..d836dd6397b 100644 --- a/django/contrib/localflavor/fr/forms.py +++ b/django/contrib/localflavor/fr/forms.py @@ -8,7 +8,7 @@ import re from django.contrib.localflavor.fr.fr_department import DEPARTMENT_CHOICES from django.core.validators import EMPTY_VALUES from django.forms import ValidationError -from django.forms.fields import Field, RegexField, Select +from django.forms.fields import CharField, RegexField, Select from django.utils.encoding import smart_unicode from django.utils.translation import ugettext_lazy as _ @@ -20,11 +20,11 @@ class FRZipCodeField(RegexField): 'invalid': _('Enter a zip code in the format XXXXX.'), } - def __init__(self, max_length=None, min_length=None, *args, **kwargs): + def __init__(self, max_length=5, min_length=5, *args, **kwargs): super(FRZipCodeField, self).__init__(r'^\d{5}$', max_length, min_length, *args, **kwargs) -class FRPhoneNumberField(Field): +class FRPhoneNumberField(CharField): """ Validate local French phone number (not international ones) The correct format is '0X XX XX XX XX'. @@ -35,6 +35,10 @@ class FRPhoneNumberField(Field): 'invalid': _('Phone numbers must be in 0X XX XX XX XX format.'), } + def __init__(self, max_length=14, min_length=10, *args, **kwargs): + super(FRPhoneNumberField, self).__init__( + max_length, min_length, *args, **kwargs) + def clean(self, value): super(FRPhoneNumberField, self).clean(value) if value in EMPTY_VALUES: @@ -51,4 +55,3 @@ class FRDepartmentSelect(Select): """ def __init__(self, attrs=None): super(FRDepartmentSelect, self).__init__(attrs, choices=DEPARTMENT_CHOICES) - diff --git a/tests/regressiontests/localflavor/fr/tests.py b/tests/regressiontests/localflavor/fr/tests.py index 55f8a68b3b9..8e99ef462bb 100644 --- a/tests/regressiontests/localflavor/fr/tests.py +++ b/tests/regressiontests/localflavor/fr/tests.py @@ -16,7 +16,8 @@ class FRLocalFlavorTests(SimpleTestCase): } invalid = { '2A200': error_format, - '980001': error_format, + '980001': ['Ensure this value has at most 5 characters (it has 6).' + ] + error_format, } self.assertFieldOutput(FRZipCodeField, valid, invalid)