2012-03-20 79 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