stefanfoulis / django-phonenumber-field

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

PhoneNumberPrefixWidget - User input is cleared (on form errors) #518

Closed Elembivios closed 2 years ago

Elembivios commented 2 years ago

When re-rendering form with errors, the user input for phone number is cleared. But only when the default region was selected. When user selected a different region than the one specified in PHONENUMBER_DEFAULT_REGION, his input persists. Entered phone number is not present even if it was valid.

At first I was certain the error lied somewhere in my code, but after building a bare bone form and view I concluded it has to be a bug. FYI I have just upgraded to django 4.1 (from 3.2) and all other packages.

I'm currently using:

My bare-bone example:

Settings.py PHONENUMBER_DEFAULT_REGION = 'SI'

Forms.py

from phonenumber_field.formfields import PhoneNumberField
from phonenumber_field.widgets import PhoneNumberPrefixWidget

class TestForm(forms.Form):
    name = forms.CharField(max_length=200, min_length=3) # Just here for triggering an invalid form
    tel = PhoneNumberField(required=False, widget=PhoneNumberPrefixWidget())

Views.py

from .forms import TestForm
class TestFormView(FormView):
    form_class= TestForm
    template_name="test_form.html"

Html


{% block content %}
<div class="container-fluid">
    <form method="POST" novalidate>
        {% csrf_token %}
        {{ form }}
        <input type="submit" name="submit">
    </form>    
</div>
{% endblock %}
francoisfreitag commented 2 years ago

Thanks for the report. I can reproduce the issue. Will work on a fix soon :)