0

In an ActiveRecord model, are there any static/canned formats I can use? For instance, when validating the format of an email address I can either make a static regex to reuse on other models or perhaps use some static that already exists in the framework.

class Contact < ActiveRecord::Base
  EMAIL_FORMAT = /^[-a-z0-9_+\.]+\@([-a-z0-9]+\.)+[a-z0-9]{2,4}$/i

  attr_accessible :first_name, :last_name, ..., :email

  validates :email, :format => EMAIL_FORMAT
end

I figured I'd be able to simply do:

validates :email, :format => ActiveRecord::SOME_STATIC_EMAIL_FORMAT

or:

validates :email, :format => :email
Josh M.
  • 26,437
  • 24
  • 119
  • 200

1 Answers1

0

Check the doc:

validates :email, format: { with: EMAIL_FORMAT }
apneadiving
  • 114,565
  • 26
  • 219
  • 213
  • I don't think that helps - I was looking for some static regex/formats in ActiveRecord I could reuse without writing new regex/formats of my own. I expected there to be a set of common formats included in the framework. The code as I have it works fine so it's no an issue of something not working, just don't want to duplicate work. – Josh M. Jan 27 '14 at 22:04
  • I always use `Devise.email_regexp` – apneadiving Jan 27 '14 at 22:44