Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Ticket #18775 - Fixed the Romanian phone number field validation rules #275

Closed
wants to merge 2 commits into from

3 participants

@danniel

The previous version only supported 10 digit phone numbers.

This branch adds validation for:

  • normal phone numbers: 0xxxxxxxxx
  • geographic short phone numbers: 0xxxxxx
  • national short phone numbers: 1xx, 1xxxxx

I ran the Django test suite and all tests pass (under SQLite).

Original ticket: https://code.djangoproject.com/ticket/18775

django/contrib/localflavor/ro/forms.py
((10 lines not shown))
default_error_messages = {
- 'invalid': _('Phone numbers must be in XXXX-XXXXXX format.'),
+ 'invalid_length':
+ _('Phone numbers may only have 7 or 10 digits, except the ' +
+ 'national short numbers which have 3 or 6 digits'),
+ 'invalid_long_format':
+ _('Normal phone numbers (7 or 10 digits) must begin with \"0\"'),
@charettes Collaborator

Are those backslashes really needed?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
django/contrib/localflavor/ro/forms.py
((10 lines not shown))
default_error_messages = {
- 'invalid': _('Phone numbers must be in XXXX-XXXXXX format.'),
+ 'invalid_length':
+ _('Phone numbers may only have 7 or 10 digits, except the ' +
+ 'national short numbers which have 3 or 6 digits'),
+ 'invalid_long_format':
+ _('Normal phone numbers (7 or 10 digits) must begin with \"0\"'),
+ 'invalid_short_format':
+ _('National short numbers (3 or 6 digits) must begin with \"1\"'),
@charettes Collaborator

Idem

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
django/contrib/localflavor/ro/forms.py
((19 lines not shown))
}
- def __init__(self, max_length=20, min_length=10, *args, **kwargs):
- super(ROPhoneNumberField, self).__init__(r'^[0-9\-\(\)\s]{10,20}$',
+ def __init__(self, max_length=20, min_length=3, *args, **kwargs):
+ super(ROPhoneNumberField, self).__init__(r'^[0-9\-\.\(\)\s]{3,20}$',
@charettes Collaborator

It might be worth compiling the regexp in the class or module level and reuse.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
django/contrib/localflavor/ro/forms.py
((5 lines not shown))
value = value.replace(' ','')
- if len(value) != 10:
- raise ValidationError(self.error_messages['invalid'])
+ length = len(value)
+ if length in [3, 6, 7, 10]:
@charettes Collaborator

This should be a tuple.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@danniel

Thanks for the tips. I will update the code and resubmit the rquest.

@adrianholovaty
Collaborator

Hey there -- django.contrib.localflavor is now deprecated, and we're not making any more changes to it. Could you reopen this pull request for the shiny new package django-localflavor-ro? Here's the link: https://github.com/django/django-localflavor-ro

Sorry we didn't get to this pull request before the deprecation. I hope it's not too much of a pain to migrate this to the new package.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.
View
43 django/contrib/localflavor/ro/forms.py
@@ -3,6 +3,7 @@
Romanian specific form helpers.
"""
from __future__ import absolute_import, unicode_literals
+import re
from django.contrib.localflavor.ro.ro_counties import COUNTIES_CHOICES
from django.core.validators import EMPTY_VALUES
@@ -10,6 +11,9 @@
from django.utils.translation import ugettext_lazy as _
+phone_digits_re = re.compile(r'^[0-9\-\.\(\)\s]{3,20}$')
+
+
class ROCIFField(RegexField):
"""
A Romanian fiscal identity code (CIF) field
@@ -47,6 +51,7 @@ def clean(self, value):
raise ValidationError(self.error_messages['invalid'])
return value[::-1]
+
class ROCNPField(RegexField):
"""
A Romanian personal identity code (CNP) field
@@ -87,6 +92,7 @@ def clean(self, value):
raise ValidationError(self.error_messages['invalid'])
return value
+
class ROCountyField(Field):
"""
A form field that validates its input is a Romanian county name or
@@ -124,6 +130,7 @@ def clean(self, value):
return entry[0]
raise ValidationError(self.error_messages['invalid'])
+
class ROCountySelect(Select):
"""
A Select widget that uses a list of Romanian counties (judete) as its
@@ -132,6 +139,7 @@ class ROCountySelect(Select):
def __init__(self, attrs=None):
super(ROCountySelect, self).__init__(attrs, choices=COUNTIES_CHOICES)
+
class ROIBANField(RegexField):
"""
Romanian International Bank Account Number (IBAN) field
@@ -155,6 +163,7 @@ def clean(self, value):
return ''
value = value.replace('-','')
value = value.replace(' ','')
+
value = value.upper()
if value[0:2] != 'RO':
raise ValidationError(self.error_messages['invalid'])
@@ -168,19 +177,28 @@ def clean(self, value):
raise ValidationError(self.error_messages['invalid'])
return value
+
class ROPhoneNumberField(RegexField):
- """Romanian phone number field"""
+ """
+ Romanian phone number field
+ """
default_error_messages = {
- 'invalid': _('Phone numbers must be in XXXX-XXXXXX format.'),
+ 'invalid_length':
+ _('Phone numbers may only have 7 or 10 digits, except the ' +
+ 'national short numbers which have 3 or 6 digits'),
+ 'invalid_long_format':
+ _('Normal phone numbers (7 or 10 digits) must begin with "0"'),
+ 'invalid_short_format':
+ _('National short numbers (3 or 6 digits) must begin with "1"'),
}
- def __init__(self, max_length=20, min_length=10, *args, **kwargs):
- super(ROPhoneNumberField, self).__init__(r'^[0-9\-\(\)\s]{10,20}$',
+ def __init__(self, max_length=20, min_length=3, *args, **kwargs):
+ super(ROPhoneNumberField, self).__init__(phone_digits_re,
max_length, min_length, *args, **kwargs)
def clean(self, value):
"""
- Strips -, (, ) and spaces. Checks the final length.
+ Strips braces, dashes, dots and spaces. Checks the final length.
"""
value = super(ROPhoneNumberField, self).clean(value)
if value in EMPTY_VALUES:
@@ -188,11 +206,21 @@ def clean(self, value):
value = value.replace('-','')
value = value.replace('(','')
value = value.replace(')','')
+ value = value.replace('.','')
value = value.replace(' ','')
- if len(value) != 10:
- raise ValidationError(self.error_messages['invalid'])
+ length = len(value)
+ if length in (3, 6, 7, 10):
+ if (length == 7 or length == 10) and value[0] != '0':
+ raise ValidationError(
+ self.error_messages['invalid_long_format'])
+ elif (length == 3 or length == 6) and value[0] != '1':
+ raise ValidationError(
+ self.error_messages['invalid_short_format'])
+ else:
+ raise ValidationError(self.error_messages['invalid_length'])
return value
+
class ROPostalCodeField(RegexField):
"""Romanian postal code field."""
default_error_messages = {
@@ -202,4 +230,3 @@ class ROPostalCodeField(RegexField):
def __init__(self, max_length=6, min_length=6, *args, **kwargs):
super(ROPostalCodeField, self).__init__(r'^[0-9][0-8][0-9]{4}$',
max_length, min_length, *args, **kwargs)
-
View
25 tests/regressiontests/localflavor/ro/tests.py
@@ -116,16 +116,27 @@ def test_ROIBANField(self):
self.assertFieldOutput(ROIBANField, valid, invalid)
def test_ROPhoneNumberField(self):
- error_format = ['Phone numbers must be in XXXX-XXXXXX format.']
- error_atleast = ['Ensure this value has at least 10 characters (it has 9).']
- error_invalid = ['Phone numbers must be in XXXX-XXXXXX format.']
+ error_invalid_length = ['Phone numbers may only have 7 or 10 digits,' +
+ ' except the national short numbers which have 3 or 6 digits']
+ error_invalid_long_format = ['Normal phone numbers (7 or 10 digits)' +
+ ' must begin with "0"']
+ error_invalid_short_format = ['National short numbers (3 or 6 digits)' +
+ ' must begin with "1"']
valid = {
- '0264485936': '0264485936',
- '(0264)-485936': '0264485936',
+ '112': '112',
+ '123.456': '123456',
+ '0232 987': '0232987',
+ '0319876543': '0319876543',
+ '031-987-6543': '0319876543',
+ '(0232) 987 654': '0232987654',
}
invalid = {
- '02644859368': error_format,
- '026448593': error_atleast + error_invalid,
+ '312': error_invalid_short_format,
+ '723.456': error_invalid_short_format,
+ '4232 987': error_invalid_long_format,
+ '4319876543': error_invalid_long_format,
+ '12345': error_invalid_length,
+ '0232 987 6543': error_invalid_length,
}
self.assertFieldOutput(ROPhoneNumberField, valid, invalid)
Something went wrong with that request. Please try again.