Skip to content

Instantly share code, notes, and snippets.

@delba
Created November 5, 2013 20:03
Show Gist options
  • Save delba/7325224 to your computer and use it in GitHub Desktop.
Save delba/7325224 to your computer and use it in GitHub Desktop.
Load custom validators in Rails 4
module Macchiato
class Application < Rails::Application
config.autoload_paths += Dir[Rails.root.join('lib', 'validators').to_s]
end
end
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