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

Optional phone number validator #129

Open
bigmassa opened this issue Nov 29, 2015 · 2 comments
Open

Optional phone number validator #129

bigmassa opened this issue Nov 29, 2015 · 2 comments

Comments

@bigmassa
Copy link

Hey Bouke,

Just want to say love this project and am currently using it on two of my sites. Fantastic work :)

Just a thought for you, what I have had to do for both projects is folk the project so I can override you validator to use my own, am still fairly new to python so know of no other way to get round this so feel free to advise.
https://github.com/bigmassa/django-two-factor-auth/commit/52f542604a1619ea6d831de71829db38bfeb5841

What ive done is following:

from django.conf import settings
from django.core.exceptions import ValidationError
from django.utils.module_loading import import_string
from django.utils.translation import ugettext_lazy as _
from phonenumber_field.phonenumber import to_python

def get_validator_class():
if getattr(settings, 'TWO_FACTOR_PHONENUMBER_VALIDATOR', None):
return import_string(settings.TWO_FACTOR_PHONENUMBER_VALIDATOR)
return validate_international_phonenumber

def validate_international_phonenumber(value):
phone_number = to_python(value)
if phone_number and not phone_number.is_valid():
raise ValidationError(validate_international_phonenumber.message,
code='invalid')

validate_international_phonenumber.message =
_('Please enter a valid phone number, including your country code '
'starting with + or 00.')

This way I can check for the likes of country codes etc on the phone number.
Just wanted to let you know incase this is something you have not thought of or it may be that I have not found a way to do it.

Once again thanks for this, a great repo :)

Stuart

@Bouke
Copy link
Collaborator

Bouke commented Dec 3, 2015

Hi Stuart. First of all, thank you! Can you elaborate on why you need additional validators, besides the newly added validation of the phonenumbers library? Maybe I'll make validating phone numbers a responsibility of the gateway, so you can customize the validation over there.

@Bouke Bouke added the question label Dec 3, 2015
@bigmassa
Copy link
Author

bigmassa commented Dec 3, 2015

Hi Bouke, it was just to only allow country code 44 (uk), thats it, but like I said it may be just that ive not come accross a better way yet.

Thanks
Stu

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

No branches or pull requests

2 participants