我有一個表單,它具有在我的本地系統中正常工作的模型驗證,但是當我在現場查看時,模型驗證順序的順序被更改,但代碼相同都。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緊急援助的驗證方法是必需如果有人有任何建議或想法。 在此先感謝
你不能使用默認的導軌驗證?你爲什麼需要你自己的?即使使用默認導軌驗證,您也可以發送自定義錯誤消息... – klump 2012-03-20 08:11:08
讓我引用ruby核心: '#當您確實需要進行有序測試時,請在測試頂部調用此函數。這樣做,你承認你吸吮,你的測試是微弱的.' 'def self.i_suck_and_my_tests_are_order_dependent!' – Reactormonk 2012-03-20 08:19:18
@klump:如何使用默認驗證的出生日期和電子郵件和電話號碼:正則表達式,我想我們必須提供自定義...如果不是,請你解釋我如何使用它們? – nidhi 2012-03-20 09:40:27