mobi/telephone_number

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

Opened this issue · 0 comments

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 :)