Skip to content

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

@kg-currenxie

Description

@kg-currenxie
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 :)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions