Skip to content

Commit

Permalink
Handle empty input to PhoneNumberPrefixWidget
Browse files Browse the repository at this point in the history
This commit changes the behavior slightly, when no phone number is
passed, the region selected by the user is reset. That’s acceptable
behavior, since the user didn’t actually enter a phone number.
  • Loading branch information
francoisfreitag committed Aug 17, 2022
1 parent 4210327 commit 4aeb786
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 19 deletions.
9 changes: 4 additions & 5 deletions phonenumber_field/widgets.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from django.conf import settings
from django.core import validators
from django.core.exceptions import ImproperlyConfigured
from django.forms import Select, TextInput
from django.forms.widgets import MultiWidget
Expand Down Expand Up @@ -90,11 +91,9 @@ def value_from_datadict(self, data, files, name):
if national_number is None:
national_number = ""
number = to_python(national_number, region=region_code)
if isinstance(number, str):
number = PhoneNumber()
number.raw_input = national_number
if not number.is_valid():
setattr(number, "_region", region_code)
if number in validators.EMPTY_VALUES:
return number
number._region = region_code
return number


Expand Down
35 changes: 21 additions & 14 deletions tests/test_widgets.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,20 +123,23 @@ def test_empty_national_number(self):
self.assertFalse(form.is_valid())
rendered_form = form.as_ul()
self.assertInHTML(
'<ul class="errorlist"><li>The phone number entered is not valid.'
"</li></ul>",
'<ul class="errorlist"><li>This field is required.</li></ul>',
rendered_form,
)
self.assertInHTML(
'<input type="text" name="phone_1" required id="id_phone_1">',
rendered_form,
count=1,
)
self.assertInHTML(
'<option value="CA" selected>Canada +1</option>',
rendered_form,
count=1,
)

def test_not_required_empty_data(self):
class TestForm(forms.Form):
phone = formfields.PhoneNumberField(
required=False, widget=PhoneNumberPrefixWidget
)

form = TestForm(data={"phone_0": "", "phone_1": ""})
self.assertIs(form.is_valid(), True)

def test_no_region(self):
form = self.form_class(data={"phone_1": "654321"})
Expand Down Expand Up @@ -164,21 +167,25 @@ def test_no_national_number(self):
self.assertFalse(form.is_valid())
rendered_form = form.as_ul()
self.assertInHTML(
'<ul class="errorlist"><li>The phone number entered is not valid.'
"</li></ul>",
rendered_form,
)
self.assertInHTML(
'<input type="text" name="phone_1" required id="id_phone_1">',
'<ul class="errorlist"><li>This field is required.</li></ul>',
rendered_form,
count=1,
)
self.assertInHTML(
'<option value="CA" selected>Canada +1</option>',
'<input type="text" name="phone_1" required id="id_phone_1">',
rendered_form,
count=1,
)

def test_not_required_no_data(self):
class TestForm(forms.Form):
phone = formfields.PhoneNumberField(
required=False, widget=PhoneNumberPrefixWidget
)

form = TestForm(data={})
self.assertIs(form.is_valid(), True)

def test_keeps_region_with_invalid_national_number(self):
form = self.form_class(data={"phone_0": "CA", "phone_1": "0000"})
self.assertFalse(form.is_valid())
Expand Down

0 comments on commit 4aeb786

Please sign in to comment.