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
Comments
This field is only wrapping Google’s https://github.com/google/libphonenumber. |
@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 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) |
Dear @KIRA009 Thanks for your reply, I hope your hack is working and will help anyone who has same problem. Thanks again |
I have closed this issue too quickly, sorry about that. Exposing an option to control accepting short numbers in the field (defaulting to |
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?
The text was updated successfully, but these errors were encountered: