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

Problem to validate short business numbers #441

Open
Ali-Javanmardi opened this issue May 3, 2021 · 4 comments · May be fixed by #603
Open

Problem to validate short business numbers #441

Ali-Javanmardi opened this issue May 3, 2021 · 4 comments · May be fixed by #603

Comments

@Ali-Javanmardi
Copy link

Hi,

I just tried django-phonenumber-field in my project and it seems so good.
but I have problem to enter short numbers specially 4 or 5 digit numbers that used to use for business call centers.

Is there any settings that let me validate such numbers or it need a hack?

@francoisfreitag
Copy link
Collaborator

This field is only wrapping Google’s https://github.com/google/libphonenumber.
If that library allows validating such numbers, then this project will. If not, this project won’t.

@KIRA009
Copy link

KIRA009 commented Dec 19, 2022

@Ali-Javanmardi Not sure if this is still helpful, but we recently faced the same issue, and the solution we figured was to override the validators and the to_python method

from django.core import validators
from django.core.exceptions import ValidationError
from django.utils.translation import gettext_lazy as _
from phonenumber_field.formfields import PhoneNumberField as PhoneNumberFormField
from phonenumber_field.modelfields import PhoneNumberField
from phonenumber_field.phonenumber import PhoneNumber, to_python
from phonenumbers import is_valid_short_number


def validate_international_phonenumber(value):
    phone_number = to_python(value)
    if isinstance(phone_number, PhoneNumber) and not (
        phone_number.is_valid() or is_valid_short_number(phone_number)
    ):
        raise ValidationError(
            _("The phone number entered is not valid."), code="invalid_phone_number"
        )


class CustomPhoneNumberFormField(PhoneNumberFormField):
    default_validators = [validate_international_phonenumber]

    def to_python(self, value):
        phone_number = to_python(value, region=self.region)

        if phone_number in validators.EMPTY_VALUES:
            return self.empty_value

        if phone_number and not (
            phone_number.is_valid() or is_valid_short_number(phone_number)
        ):
            raise ValidationError(self.error_messages["invalid"])

        return phone_number


class CustomPhoneNumberField(PhoneNumberField):
    validators = [validate_international_phonenumber]

    def formfield(self, **kwargs):
        overrides = {
            "form_class": CustomPhoneNumberFormField,
        }
        kwargs.update(overrides)
        return super().formfield(**kwargs)

@Ali-Javanmardi
Copy link
Author

Dear @KIRA009

Thanks for your reply,
This question was for a long time ago and I ignored using any special field for phone-number in my project.

I hope your hack is working and will help anyone who has same problem.
I will test your suggested code as soon as I create a test project to use this field type again.

Thanks again

@francoisfreitag
Copy link
Collaborator

francoisfreitag commented Jan 3, 2023

I have closed this issue too quickly, sorry about that. Exposing an option to control accepting short numbers in the field (defaulting to False for BC), that updates the PhoneNumber.is_valid() logic to accept valid short numbers (is_valid_short_number) would make sense.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants