Skip to content

Commit

Permalink
Make formfields.PhoneNumberField honor PHONENUMBER_DEFAULT_REGION
Browse files Browse the repository at this point in the history
Reviewed-by: stefanfoulis
  • Loading branch information
francoisfreitag committed Dec 7, 2021
1 parent 818a1c0 commit 1da0b6a
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 1 deletion.
3 changes: 3 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ CHANGELOG
UNRELEASED
----------

* Set the default region of ``phonenumber_field.formfields.PhoneNumberField``
to ``PHONENUMER_DEFAULT_REGION``.

6.0.0 (2021-10-20)
------------------

Expand Down
3 changes: 2 additions & 1 deletion phonenumber_field/formfields.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import phonenumbers
from django.conf import settings
from django.core import validators
from django.core.exceptions import ValidationError
from django.forms.fields import CharField
Expand All @@ -17,7 +18,7 @@ def __init__(self, *args, region=None, **kwargs):
self.widget.input_type = "tel"

validate_region(region)
self.region = region
self.region = region or getattr(settings, "PHONENUMBER_DEFAULT_REGION", None)

if "invalid" not in self.error_messages:
if region:
Expand Down
8 changes: 8 additions & 0 deletions tests/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -431,6 +431,14 @@ class PhoneNumberForm(forms.Form):
},
)

@override_settings(PHONENUMBER_DEFAULT_REGION="FR")
def test_formfield_uses_default_region(self):
class PhoneForm(forms.Form):
phone_number = formfields.PhoneNumberField()

form = PhoneForm()
self.assertEqual("FR", form.fields["phone_number"].region)


class RegionPhoneNumberModelFieldTest(TestCase):
def test_uses_region(self):
Expand Down

0 comments on commit 1da0b6a

Please sign in to comment.