Ruby on rails RubyonRails更改自定义模型验证的顺序
我有一个表单,它有模型验证,在我的本地系统中可以正常工作,但是当我在实时站点上检查它时,模型验证顺序会发生变化,尽管两者的代码相同 这是模型中的代码块:Ruby on rails RubyonRails更改自定义模型验证的顺序,ruby-on-rails,validation,model,Ruby On Rails,Validation,Model,我有一个表单,它有模型验证,在我的本地系统中可以正常工作,但是当我在实时站点上检查它时,模型验证顺序会发生变化,尽管两者的代码相同 这是模型中的代码块: def validate #email validation if !email.blank? #errors.add(:email,I18n.t(:ismissing)) #else if email != email_confirmation errors.add(:e
def validate
#email validation
if !email.blank?
#errors.add(:email,I18n.t(:ismissing))
#else
if email != email_confirmation
errors.add(:email,I18n.t(:ErrorMessageConfirmEmailNotmatch))
else
if email.length <=200 then
if email.match(/^[^@][\w.-]*@[\w.-]+[.][a-z]{2,4}$/i).nil?
errors.add(:email,I18n.t(:ErrorMessageInvalid))
else
if @new_record==true
if User.find(:all, :conditions => ['lower(email) = ?', email.downcase]).count>0
#errors.add(:email," ID already exists. Provide another Email ID")
errors.add(:email,I18n.t(:ErrorMessageAlreadyExists))
end
else
if @changed_attributes["email"]!=nil
if User.User.find(:all, :conditions => ['lower(email) = ?', email.downcase]).count>0
#errors.add(:email," ID already exists. Provide another Email ID")
errors.add(:email,I18n.t(:ErrorMessageAlreadyExists))
end
end
end
end
else
errors.add(:email, I18n.t(:ErroeMessageMustlessthen,:size=>200))
end
end
else
errors.add(:email,I18n.t(:ismissing))
end
#end : Email validation
if email_confirmation.blank?
errors.add(:email_confirmation,I18n.t(:ismissing))
end
#pasword validation
if @new_record==true
if password.blank?
errors.add(:password,I18n.t(:ismissing))
else
if password_confirmation != password
errors.add(:password,I18n.t(:ErrorMessageConfirmPasswordNotmatch))
end
if !password.nil?
if password.length < 4 || password.length > 50 then
errors.add(:password,I18n.t(:ErroeMessageShouldBetween,:from=>"4",:to=>"50"))
end
errors.add(:password,I18n.t(:ErrorMessageInvalidPassword)) if password.match('^[a-z0-9@#*-_]*$').nil?
end
end
end
#end password validation
if @new_record==true
if password_confirmation.blank?
errors.add(:password_confirmation,I18n.t(:ismissing))
end
end
if dob.blank?
errors.add(:dob,I18n.t(:ErrorMessageInvalid))
else
begin
#dt = DateTime.strptime(dob, "%m/%d/%Y").to_date
if dob.year <= 1900 then
errors.add(:dob,I18n.t(:ErrorMessageInvalidYear))
end
if dob>=Date.today then
errors.add(:dob,I18n.t(:ErroeMessageInvalidBirthday))
end
rescue Exception => ex
#errors.add(:dob,'is Invalid (MM/DD/YYYY format)')
errors.add(:dob,I18n.t(:ErroeMessageInvalidBirthday))
end
end
end
def验证
#电子邮件验证
如果!email.blank?
#错误。添加(:电子邮件,I18n.t(:ismissing))
#否则
如果是电子邮件!=电子邮件确认
errors.add(:email,I18n.t(:ErrorMessageConfirmEmailNotmatch))
其他的
如果email.length['lower(email)=?',email.downcase]).count>0
#错误。添加(:电子邮件,“ID已存在。请提供另一个电子邮件ID”)
errors.add(:email,I18n.t(:ErrorMessageAlreadyExists))
结束
其他的
如果@changed_属性[“email”]=无
如果User.User.find(:all,:conditions=>['lower(email)=?',email.downcase])。计数>0
#错误。添加(:电子邮件,“ID已存在。请提供另一个电子邮件ID”)
errors.add(:email,I18n.t(:ErrorMessageAlreadyExists))
结束
结束
结束
结束
其他的
errors.add(:email,I18n.t(:erromeMessageCustomlessthen,:size=>200))
结束
结束
其他的
错误。添加(:电子邮件,I18n.t(:ismissing))
结束
#结束:电子邮件验证
如果电子邮件为空?
错误。添加(:电子邮件\u确认,I18n.t(:ismissing))
结束
#密码验证
如果@new_record==true
如果password.blank?
错误。添加(:密码,I18n.t(:ismissing))
其他的
如果密码未确认!=密码
errors.add(:password,I18n.t(:ErrorMessageConfirmPasswordNotmatch))
结束
如果!密码。零?
如果password.length<4 | | password.length>50,则
errors.add(:password,I18n.t(:erromeMessageShouldBetween,:from=>“4”,“to=>“50”))
结束
错误。如果password.match(“^[a-z0-9@#*-#]*$”),则添加(:password,I18n.t(:ErrorMessageInvalidPassword))。无?
结束
结束
结束
#结束密码验证
如果@new_record==true
如果密码\u confirmation.blank?
错误。添加(:密码\u确认,I18n.t(:ismissing))
结束
结束
如果dob.blank?
errors.add(:dob,I18n.t(:ErrorMessageInvalid))
其他的
开始
#dt=DateTime.strtime(dob,“%m/%d/%Y”)。截止日期
如果dob.year=Date.today,则
错误.add(:dob,I18n.t(:erromeMessageInValidBirthday))
结束
营救异常=>ex
#错误。添加(:dob,'无效(MM/DD/YYYY格式)')
错误.add(:dob,I18n.t(:erromeMessageInValidBirthday))
结束
结束
结束
控制器在注册时调用验证方法。如果有人有任何建议或想法,需要紧急帮助。
提前感谢我不知道为什么这些程序不能按顺序执行。您是否在生产中记录了一些指示 与其将所有内容都放在一个大的validate方法中,不如将其分成几个(通常可能是更好的做法),然后按您想要的顺序调用 例如
您可以使用rails默认验证..我对电子邮件进行了验证,并在此处提供了示例..
validates :email,
:presence =>{ :message => I18n.t(:ismissing)},
:length => {:allow_blank => true, :maximum => 200, :message => I18n.t(:ErroeMessageMustlessthen,:size=>200)},
:format => {:allow_blank => true, :with => /^[^@][\w.-]*@[\w.-]+[.][a-z]{2,4}$/i, :message => I18n.t(:ErrorMess
:uniqueness => {:allow_blank => true, :message => I18n.t(:ErrorMessageAlreadyExists)},
:confirmation => {:message => I18n.t(:ErrorMessageConfirmEmailNotmatch)}
同样,您也可以对其他字段执行此操作。您不能使用默认的rails验证吗?你为什么需要自己的?即使在使用默认的rails验证时,您也可以发送自定义错误消息……让我引用ruby core:
#当您确实需要进行有序测试时,在测试的顶部调用此命令。这样做,你承认你很糟糕,你的测试很弱。
def self.i_suck_和_my_tests_依赖顺序代码>@klump:如何对出生日期、电子邮件和电话使用默认验证no:regular expressions,我想我们必须提供自定义…如果没有,请解释一下我如何使用它们?@tass:我不明白你想说什么….@user1280137:请看下面斯大林的答案,我会做一些像himHi Stalin一样的事情:验证工作,但是当我对多个字段进行验证时,它们的显示顺序仍然不符合顺序……你好,Kevin……如果我拆分它们,验证不会被解雇。是否有其他方法来维护它们的顺序
validates :email,
:presence =>{ :message => I18n.t(:ismissing)},
:length => {:allow_blank => true, :maximum => 200, :message => I18n.t(:ErroeMessageMustlessthen,:size=>200)},
:format => {:allow_blank => true, :with => /^[^@][\w.-]*@[\w.-]+[.][a-z]{2,4}$/i, :message => I18n.t(:ErrorMess
:uniqueness => {:allow_blank => true, :message => I18n.t(:ErrorMessageAlreadyExists)},
:confirmation => {:message => I18n.t(:ErrorMessageConfirmEmailNotmatch)}