Skip to content

Support use in Rails validations #47

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

Open
elskwid opened this issue Nov 17, 2013 · 2 comments
Open

Support use in Rails validations #47

elskwid opened this issue Nov 17, 2013 · 2 comments

Comments

@elskwid
Copy link
Collaborator

elskwid commented Nov 17, 2013

#17 was asking how to use phone in model validations. This would be a nice feature but it isn't on the roadmap.

@milgner
Copy link

milgner commented Aug 26, 2014

Actually this is quite simple, posting just in case someone is looking for it:

class PhoneNumberValidator < ActiveModel::EachValidator
  def validate_each(record, attribute, value)
    return if value.nil? || value.empty? || Phoner::Phone.valid?(value)
    record.errors.add(attribute, I18n.t('errors.messages.invalid'))
  end
end

And then in your model:

class User < ActiveRecord::Base
  validates :phone_number, phone_number: true
end

@elskwid
Copy link
Collaborator Author

elskwid commented Jan 31, 2015

@milgner do you mind if I add something like this to the README? (Or you could too. 😄)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants