stefanfoulis / django-phonenumber-field

A django model and form field for normalised phone numbers using python-phonenumbers
MIT License
1.48k stars 318 forks source link

Render input value with PhoneNumberPrefixWidget on form errors #520

Closed francoisfreitag closed 2 years ago

francoisfreitag commented 2 years ago

The phone number input field was incorrectly cleared when:

That behavior was caused by 005769cf39323e5b23710783f45befb546672cd6, which incorrectly used prepare_value() to transform a PhoneNumber instance to an str (the phone number in national format). By doing so, information was lost and the decompress() method of PhoneNumberPrefixWidget unexpectedly received an str.

The phone number value is now always a PhoneNumber instance. It’s up to the widget to format that value. To retain the behavior of showing national phone numbers in the national format by default, the formfields.PhoneNumberField widget is now PhoneNumberInternationalFallbackWidget.

PhoneNumberInternationalFallbackWidget behavior was updated to match the previous implementation of prepare_value: the number is displayed in national format if its country code matches the country code of the configured region. It now represents its value as a PhoneNumber, a richer type than str, which facilitates the custom display logic.

Fixes #518

francoisfreitag commented 2 years ago

We should bump the release to 7.0.0 with this, due to the change of the formfields.PhoneNumberField default widget to PhoneNumberInternationalWidget, which may introduce some subtle backward incompatibilities.


Django 2.2 failures can be ignored, the version is dead. See https://github.com/stefanfoulis/django-phonenumber-field/pull/515.

francoisfreitag commented 2 years ago

CC @amateja, that explains why decompress() used to receive an str instance.