我想爲我的應用程序添加一個電子郵件自定義驗證器;但是,我應該在哪裏放置自定義驗證器? (我真的不想把這個驗證器類放在模型中)是否有驗證器的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 –