Skip to content

Instantly share code, notes, and snippets.

@PerezIgnacio
Created February 4, 2020 12:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save PerezIgnacio/5a5358b69439587dc3d8c5bb3dbf126e to your computer and use it in GitHub Desktop.
Save PerezIgnacio/5a5358b69439587dc3d8c5bb3dbf126e to your computer and use it in GitHub Desktop.
EachValidator example
class EmailValidator < ActiveModel::EachValidator
def validate_each(record, attribute, value)
return if value =~ /\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\z/i
record.errors[attribute] << (options[:message] || 'is not an email')
end
end
class Person < ApplicationRecord
validates :email, presence: true, email: true
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment