Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move validation from widgets to the form fields #603

Merged
merged 3 commits into from
Jun 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
219 changes: 134 additions & 85 deletions docs/reference.rst
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,12 @@
def print_html(html):
print(BeautifulSoup(html).prettify().strip())

def print_html_compact(html):
output = BeautifulSoup(html).prettify().strip().splitlines()
print("\n".join(output[:15]))
print(" ...")
print("\n".join(output[-7:]))


The PhoneNumber wrapper
=======================
Expand Down Expand Up @@ -85,8 +91,11 @@ Usage
# An optional phone number.
phone_number = PhoneNumberField(blank=True)

Form field
==========
Form fields
===========

:class:`~phonenumber_field.formfields.PhoneNumberField`
-------------------------------------------------------

The :class:`~phonenumber_field.formfields.PhoneNumberField` :ref:`form field
<ref/forms/fields:form fields>` to validate
Expand All @@ -104,7 +113,7 @@ The :class:`~phonenumber_field.formfields.PhoneNumberField` :ref:`form field
.. automethod:: __init__

Usage
-----
~~~~~

.. doctest:: formfield

Expand Down Expand Up @@ -151,6 +160,127 @@ Usage
international example phone number in the E.164 format is suggested.


:class:`~phonenumber_field.formfields.SplitPhoneNumberField`
------------------------------------------------------------

A :class:`~django.forms.MultiValueField` that offers:

- a ``<select … >`` element to choose the region, and
- an ``<input type="tel" … >`` to enter the phone number.

This widget uses an example phone number from the selected region for the
``invalid`` key in :attr:`~django.forms.Field.error_messages`, when the region
choice is valid.

To customize the dynamic message, use
:func:`phonenumber_field.formfields.SplitPhoneNumberField.invalid_error_message`.

.. important:: Requires the `Babel <https://pypi.org/project/Babel/>`_ package.

.. autoclass:: phonenumber_field.formfields.SplitPhoneNumberField

.. automethod:: __init__
.. automethod:: invalid_error_message

Usage
~~~~~

.. doctest:: SplitPhoneNumberField

>>> from django import forms
>>> from phonenumber_field.formfields import SplitPhoneNumberField

>>> class PhoneForm(forms.Form):
... number = SplitPhoneNumberField()
...

>>> form = PhoneForm()
>>> print_html_compact(form.as_div())
<div>
<fieldset>
<legend>
Number:
</legend>
<select id="id_number_0" name="number_0" required="">
<option selected="" value="">
---------
</option>
<option value="AF">
Afghanistan +93
</option>
<option value="AL">
Albania +355
</option>
...
<option value="AX">
Åland Islands +358
</option>
</select>
<input id="id_number_1" name="number_1" required="" type="tel"/>
</fieldset>
</div>

# Limiting country choices.
>>> class DemoSplitPhoneNumberField(SplitPhoneNumberField):
... def prefix_field(self):
... return forms.ChoiceField(choices=[
... ("", "---------"),
... ("CA", "Canada"),
... ("FR", "France"),
... ])
...
>>> class LimitedCountryPhoneForm(forms.Form):
... number = DemoSplitPhoneNumberField()
...
>>> form = LimitedCountryPhoneForm()
>>> print_html(form.as_div())
<div>
<fieldset>
<legend>
Number:
</legend>
<select id="id_number_0" name="number_0" required="">
<option selected="" value="">
---------
</option>
<option value="CA">
Canada
</option>
<option value="FR">
France
</option>
</select>
<input id="id_number_1" name="number_1" required="" type="tel"/>
</fieldset>
</div>

# Pre-selecting a country.
>>> class FrenchPhoneForm(forms.Form):
... number = DemoSplitPhoneNumberField(region="FR")
...

>>> form = FrenchPhoneForm()
>>> print_html(form.as_div())
<div>
<fieldset>
<legend>
Number:
</legend>
<select id="id_number_0" name="number_0" required="">
<option value="">
---------
</option>
<option value="CA">
Canada
</option>
<option selected="" value="FR">
France
</option>
</select>
<input id="id_number_1" name="number_1" required="" type="tel"/>
</fieldset>
</div>

Widgets
-------

Expand Down Expand Up @@ -209,89 +339,8 @@ Usage
PhoneNumberPrefixWidget
~~~~~~~~~~~~~~~~~~~~~~~

.. important:: Requires the `Babel <https://pypi.org/project/Babel/>`_ package
be installed.

.. autoclass:: phonenumber_field.widgets.PhoneNumberPrefixWidget

.. automethod:: __init__

Usage
.....

.. doctest:: prefixwidget

>>> from django import forms
>>> from phonenumber_field.formfields import PhoneNumberField
>>> from phonenumber_field.widgets import PhoneNumberPrefixWidget

# Limiting country choices.
>>> class CanadianPhoneForm(forms.Form):
... # RegionalPhoneNumberWidget is the default widget.
... number = PhoneNumberField(
... region="CA",
... widget=PhoneNumberPrefixWidget(
... country_choices=[
... ("CA", "Canada"),
... ("FR", "France"),
... ],
... ),
... )
...

>>> form = CanadianPhoneForm({"number_0": "CA", "number_1": "6044011234"})
>>> print_html(form.as_div())
<div>
<fieldset>
<legend>
Number:
</legend>
<select id="id_number_0" name="number_0" required="">
<option selected="" value="CA">
Canada
</option>
<option value="FR">
France
</option>
</select>
<input id="id_number_1" name="number_1" required="" type="text" value="6044011234"/>
</fieldset>
</div>

# Pre-selecting a country.
>>> class FrenchPhoneForm(forms.Form):
... # RegionalPhoneNumberWidget is the default widget.
... number = PhoneNumberField(
... region="FR",
... widget=PhoneNumberPrefixWidget(
... initial="FR",
... country_choices=[
... ("CA", "Canada"),
... ("FR", "France"),
... ],
... ),
... )
...

>>> form = FrenchPhoneForm()
>>> print_html(form.as_div())
<div>
<fieldset>
<legend>
Number:
</legend>
<select id="id_number_0" name="number_0" required="">
<option value="CA">
Canada
</option>
<option selected="" value="FR">
France
</option>
</select>
<input id="id_number_1" name="number_1" required="" type="text"/>
</fieldset>
</div>

Serializer field
================

Expand Down Expand Up @@ -350,7 +399,7 @@ Validates:

.. autofunction:: phonenumber_field.validators.validate_international_phonenumber

**code**: ``"invalid_phone_number"``
**code**: ``"invalid"``

Settings
========
Expand Down
Loading
Loading