Created
November 5, 2013 20:03
-
-
Save delba/7325224 to your computer and use it in GitHub Desktop.
Load custom validators in Rails 4
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module Macchiato | |
class Application < Rails::Application | |
config.autoload_paths += Dir[Rails.root.join('lib', 'validators').to_s] | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class EmailValidator < ActiveModel::EachValidator | |
def validate_each(record, attribute, value) | |
unless value =~ /\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\z/i | |
record.errors[attribute] << (options[:message] || "is not an email") | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment