I am following Ryan Bates' Railscast. I find when I tried to establish my customized field validator, my rails 3 is not working as expected.
I established a new email_format_validator.rb file in under lib/ and the codes are:
class EmailFormatValidator < ActiveModel::EachValidator
def validate_each(object, attribute, value)
unless value =~ /^([^@s]+)@((?:[-a-z0-9]+.)+[a-z]{2,})$/i
object.errors[attribute] << (options[:message] || "is not formatted properly")
end
end
end
I put this line in my user.rb (Model file):
validates :email, :presence => true, :uniqueness => true, :email_format=>true
The browser complained:
Unknown validator: 'email_format'
Why? How to solve this?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…