我想为我的应用程序添加一个电子邮件自定义验证器;但是,我应该在哪里放置自定义验证器? (我真的不想把这个验证器类放在模型中)是否有验证器的cli生成器?我应该在Rails 5中放置自定义验证器?
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
什么是用于自定义验证公约位置/路径?
的是,在轨道公约?并将它自动加载? –
显然[SO:5263239](http://stackoverflow.com/questions/5263239/where-should-rails-3-custom-validators-be-stored),[SO:35953656](http://stackoverflow.com#/questions/35953656/where-exactly-do-i-put-autoload-path-for-custom-validator-in-config-application) – devoh
感谢您的额外信息。我只使用api导轨。 :D –