I am trying to add custom email validators for my application; However, where should I place a custom validator? (I really don't want to put this validation class in the model) Is there a cli generator for the validator?
http://guides.rubyonrails.org/active_record_validations.html
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
class Person < ApplicationRecord
validates :email, presence: true, email: true
end
What is the location / convention path for a custom validator?
source
share