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

6.1.0 -> 6.2.0 form invalid #511

Closed shawnngtq closed 2 years ago

shawnngtq commented 2 years ago

After updating from 6.1.0 to 6.2.0, my phonenumber inlineformset_factory throws form invalid for empty field.

The phone number entered is not valid.

# models.py
    phone_number = PhoneNumberField(
        help_text=_("Numeric values"),
    )
# forms.py
class PhoneNumberForm(forms.ModelForm):
    class Meta(MetadataFormWidget):
        model = PhoneNumber
        widgets = {
            "phone_number": PhoneNumberPrefixWidget(
                attrs={"class": "form-control", "placeholder": "8882804331"},
            ),
        }

PhoneNumberFormSet = forms.models.inlineformset_factory(
    OtherModel,
    PhoneNumber,
    form=PhoneNumberForm,
    can_delete=False,
    extra=1,
)

If I revert to 6.1.0 OR exclude phone_number field, problem disappear

I suspect it's due to update in validators.py / widgets.py

https://github.com/stefanfoulis/django-phonenumber-field/compare/6.1.0...6.2.0

shawnngtq commented 2 years ago
# models.py
    phone_number = PhoneNumberField(
        blank=True,
        help_text=_("Numeric values"),
        null=True,
    )

Still invalid even if I set blank and null true

francoisfreitag commented 2 years ago

Thanks for the report, I can reproduce the problem with the following form:

        class TestForm(forms.Form):
            phone = formfields.PhoneNumberField(
                required=False, widget=PhoneNumberPrefixWidget
            )
shawnngtq commented 2 years ago

@francoisfreitag , how can I do monkey patch locally for this? for testing purpose only

Will it work if I create the same directory structure, location and validators.py, then just modify that file?

If monkey patch is not recommended / impossible, what is the recommended way?

So that next time I can give better debug and maybe create pull request.

francoisfreitag commented 2 years ago

I used the project test suite to reproduce the issue: https://github.com/stefanfoulis/django-phonenumber-field/pull/512/files#diff-af3529af46c0c6d7c1c94af472ea2a6b21b19cc715c2c7207e53f1c8f7fc9bdaR135-R142

You should be able to pip install a pull request to experiment locally with:

pip install git+https://github.com/stefanfoulis/django-phonenumber-field.git@refs/pull/512/head