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

Phone invalid when using [:mobile] without country code. And validator auto detect? #131

Open
kg-currenxie opened this issue Nov 2, 2023 · 0 comments

Comments

@kg-currenxie
Copy link

kg-currenxie commented Nov 2, 2023

irb(main):022> TelephoneNumber.valid?('+85290191054')
=> true
irb(main):023> TelephoneNumber.valid?('+85290191054', [:mobile])
=> false
irb(main):024> TelephoneNumber.valid?('+85290191054', 'HK', [:mobile])
=> true

Is this by design? 🤔

My scenario:

  • User types phone number in 1 field (no country selector), manually, including their prefix
  • I want to auto-detect the country, and then also validate it

So, I'm using the validator like so:

validates :mobile_number, telephone_number: { country: proc { |record| TelephoneNumber.parse(record.mobile_number).country&.country_id }, types: [:mobile] }

This works fine, but would it be possible to auto-detect like i do here, but without specifying the country option, or another attribute like detect_country: true?

Thanks :)

@kg-currenxie kg-currenxie changed the title Phone invalid when using [:mobile] without country code Phone invalid when using [:mobile] without country code. And validator auto detect? Nov 2, 2023
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

1 participant