繁体   English   中英

Rails,with_options块中的验证不起作用

[英]Rails, validations within a with_options block don't work

我有一个旧的Rails 2应用程序,其中有一个User模型,可以处理来自Web和移动应用程序的用户配置文件。 我想为网络和移动用户提供2个独立的验证组。 我使用DEVISE进行身份验证。

为了如果用户涉及网络或移动识别,我使用称为validation_for_mobile我设置在控制器侧,保存/更新记录之前的attr_accessor。

看起来有些验证,尤其是“ validates_length_of:password ”,即使它们位于不应该评估的with_options块中,也会被执行。

在用户模型内部,我有以下代码:

# WEB VALIDATIONS
with_options :if => "self.validation_for_mobile == false && !self.remote_sync" do |vm1|
    vm1.validates_date :birthday, :allow_blank => true
    vm1.validates_presence_of :company, :name, :surname, :address, :city, :province, :postal_code, :position, :company_type, :phone, :unless => :is_imported
    vm1.validates_format_of :phone, :with => /\A([0-9]+)\Z/i, :message => 'deve contenere solo numeri', :unless => :is_imported

    vm1.validates_presence_of :vat, :if => "!imported && !self.script_imported && company_type != 'individuale' && !company_type.blank?"
    vm1.validates_presence_of :social_number, :if => "!imported && !self.script_imported && company_type == 'individuale' && !company_type.blank? "
    vm1.validates_presence_of  :remote_id, :if => "!came_from_user && !imported && !script_imported"
    vm1.validates_acceptance_of :privacy_accepted, :privacy_third_part, :accept => true, :allow_nil => false, :unless => :is_imported
    vm1.validates_presence_of :login, :email
    vm1.validates_email_format_of :email, :message => "email non valida"
    vm1.validates_presence_of :remote_sap_code, :remote_as_code, :if => "!came_from_user"
    vm1.validates_uniqueness_of :login, :scope => :branch_id, :case_sensitive => false
    vm1.with_options :if => :password_required? do |v|
      v.validates_presence_of :password
      v.validates_confirmation_of :password
      v.validates_format_of :password, :with => /^(?=(.*\d){1})(?=.*[a-zA-Z])[0-9a-zA-Z]+$/i, :message => 'solo lettere e almeno 1 numero!'
      v.validates_length_of :password, :within => 6..10, :allow_blank => true
    end
  end


# MOBILE VALIDATIONS
with_options :if => "self.validation_for_mobile == true && !self.remote_sync" do |vm2|
  with_options :if => :mobile_validations do |vm2|
    vm2.validates_presence_of :company, :vat, :name, :surname, :phone
    vm2.validates_format_of :phone, :with => /\A([0-9]+)\Z/i, :message => 'deve contenere solo numeri'
    vm2.validates_length_of :vat, :within => 11..15
    vm2.validates_presence_of :login, :email, :branch_id
    vm2.validates_email_format_of :email, :message => "email non valida"
    vm2.validates_uniqueness_of :login, :scope => :branch_id, :case_sensitive => false
    vm2.with_options :if => :password_required? do |v|
      v.validates_presence_of :password
      v.validates_confirmation_of :password
      v.validates_format_of :password, :with => /^(?=(.*\d){1})(?=.*[a-zA-Z])[0-9a-zA-Z]+$/i, :message => 'solo lettere e almeno 1 numero!'
      v.validates_length_of :password, :within => 1..10, :allow_blank => true
    end
  end

每次尝试通过移动设备创建新用户时,我都希望仅评估第二个验证块,其中“ validates_length_of:password在=> 1..10之内是“

但是我收到密码太短(至少6个字符)的错误消息。 那是第一个街区!!!

你能帮助我吗?

我还尝试将with_options的条件移入方法中,如下所示:

with_options :if => :web_validations
with_options :if => :mobile_validations

但是什么都没有改变。

这是我用来测试的代码:

@user = User.new
@user.company = "Testuser"
@user.vat = "123456789012"
@user.name = "Fafag"
@user.surname = "Fafag"
@user.phone = "9182624"
@user.email = "utentest9876@test.it"
@user.login = "Utentest63"
@user.branch_id = 2

@user.password = "TEST1"
@user.password_confirmation = "TEST1"
@user.plain_password = "TEST1"

@user.mobile_signup = true
@user.mobile = true
@user.validation_for_mobile = true    # model custom validation
@user.renew_mobile_token!

@user.came_from_user = true
@user.remote_id = 0
@user.confirmed = nil
@user.active = false

if @user.save
  return true
else
  puts @user.errors.full_messages.inspect
end

好的,我解决了这个难题!

嵌套的with_options块似乎不继承外部条件。 因此,我必须在嵌套的with_option块中指定第一个条件,如下所示:

with_options :if => :mobile_validations do |vm2|
    vm2.validates_presence_of :company, :vat, :name, :surname, :phone
    vm2.validates_format_of :phone, :with => /\A([0-9]+)\Z/i, :message => 'deve contenere solo numeri'
    vm2.validates_length_of :vat, :within => 11..15
    vm2.validates_presence_of :login, :email, :branch_id
    vm2.validates_email_format_of :email, :message => "email non valida"
    vm2.validates_uniqueness_of :login, :scope => :branch_id, :case_sensitive => false
    vm2.with_options :if => [:password_required?, :mobile_validations] do |v2|
      v2.validates_presence_of :password
      v2.validates_confirmation_of :password
      v2.validates_format_of :password, :with => /^(?=(.*\d){1})(?=.*[a-zA-Z])[0-9a-zA-Z]+$/i, :message => 'solo lettere e almeno 1 numero!'
      v2.validates_length_of :password, :within => 1..10, :allow_blank => true
    end
  end

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM