2012-03-20 76 views
0

我有一个表单,它具有在我的本地系统中正常工作的模型验证,但是当我在现场查看时,模型验证顺序的顺序被更改,但代码相同都。Ruby on Rails自定义模型验证的更改顺序

这是代码模型块:

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('^[[email protected]#*-_]*$').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 

和控制器呼吁registration.An紧急援助的验证方法是必需如果有人有任何建议或想法。 在此先感谢

+0

你不能使用默认的导轨验证?你为什么需要你自己的?即使使用默认导轨验证,您也可以发送自定义错误消息... – klump 2012-03-20 08:11:08

+0

让我引用ruby核心: '#当您确实需要进行有序测试时,请在测试顶部调用此函数。这样做,你承认你吸吮,你的测试是微弱的.' 'def self.i_suck_and_my_tests_are_order_dependent!' – Reactormonk 2012-03-20 08:19:18

+0

@klump:如何使用默认验证的出生日期和电子邮件和电话号码:正则表达式,我想我们必须提供自定义...如果不是,请你解释我如何使用它们? – nidhi 2012-03-20 09:40:27

回答

0

不知道为什么这些不会按顺序执行。你有没有登录过某些东西来表明在生产中?

而不是把所有的东西放在一个大的验证方法中,也许分成几个(一般来说可能是一个更好的做法),然后按照你想要的顺序调用。

例如。

before_save :validate_email, :validate_dob 


def validate_email 
... 
end 

def validate_dob 
... 
end 
+0

你好凯文...验证不会被解雇,如果我拆分他们有一些其他方式来维护他们的订单 – nidhi 2012-03-20 10:56:58

2

您可以使用轨道默认validations..I没有为电子邮件,在这里给你的样品..

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)} 

同样也可以做其他领域。

+0

嗨斯大林:验证工程,但当我对多个字段进行验证时,它们的显示顺序仍然不正确 – nidhi 2012-03-20 10:31:38