我有一个表单,它具有在我的本地系统中正常工作的模型验证,但是当我在现场查看时,模型验证顺序的顺序被更改,但代码相同都。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