本文介绍了轨道4活动记录验证 - 有条件地验证了4个属性presence如果至少有一个是同时允许无以present present的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有10个属性的一种形式。

其中我有4个属性,我需要办理什么我称之为互惠条件presence活动记录验证。

我要那个

  • (A)如果一个人至少是present,那么其他3必须是present
  • (B)仍允许无以present(如果其他3是空白的,那么第四个拥有可为空右)

下面是四个属性:

  • 在地址行
  • 拉链code
  • 状态
  • 国家

这意味着如果用户填写其中之一则所有其他需要成为present。但是,如果所有的都是空白,它的确定。

到目前为止,我只能够管理强制执行(A)。但我不执行(B)。

事实上,当我试图把allow_blank:真正的上的4个属性验证,那么它打破(A)之一,因为在那之后,它并不能保证,如果在属性为present,其他人必须是为好。

如何做到这一点?

下面是我目前的code

规格/型号/使用者

 验证:地址行,
              presence:的确,如果:pa_subelements_mutual_ presence?
              长度:{最大:100,
                        最低:3}
  验证:拉链code,
              presence:的确,如果:pa_subelements_mutual_ presence?
              长度:{最大值:20,
                        最低:4}
  验证:状态,
              presence:的确,如果:pa_subelements_mutual_ presence?
  验证:国家,
              presence:的确,如果:pa_subelements_mutual_ presence?
              长度:{最大值:50}

私人
 高清pa_subelements_mutual_ presence? #方法来帮助验证集上相互依存为presence邮政地址
      拉姆达{self.address_line_1。present? } ||
      拉姆达{self.zip code,present? } ||
      拉姆达{self.state。present? } ||
      拉姆达{self.country。present? }
    结束
 

解决方案

在我看来,它是所有四个或没有。没有测试,但这应该工作。

 验证:all_or_none

私人
    高清all_or_none
        错误[:基地]<< 全有或全无,纨绔子弟,除非
            (address_line_1.blank&安培;?&安培;拉链code.blank和放大器;?&安培; state.blank和放大器;&放大器; country.blank?)||
            (address_line_1.blank和放大器;!?&放大器;!拉链code.blank和放大器;?&安培;!state.blank和放大器;?&安培;!country.blank?)
    结束
 

all_or_none 要么如果四个字段为空或者其中没有一个是真的。

I have a form with 10 attributes.

Among them I have 4 attributes which I need to apply what I'd call a"mutually conditional presence" Active Record validation.

I want that

  • (A) if one at least is present, then the other 3 must be present
  • (B) still allow none to be present (if the other 3 are blank, then the fourth has the right be be blank)

Here are the four attributes:

  • address_line_1
  • zipcode
  • state
  • country

It means that if the user fills ONE of them then ALL the others have to be present. But if all are blank, it's ok.

So far I have only be able to manage to enforce (A). But I fail to implement (B).

Indeed when i try putting allow_blank: true on one of the 4 attributes validates, then it breaks (A) , as after that, it does not ensure that if on of the attributes is present, the others must be as well.

How to do this?

Here is my current code

spec/models/users

validates :address_line_1,
              presence: true, if: :pa_subelements_mutual_presence?
              length: { maximum: 100,
                        minimum: 3 }
  validates :zipcode, 
              presence: true, if: :pa_subelements_mutual_presence?,
              length: { maximum: 20,
                        minimum: 4} 
  validates :state, 
              presence: true, if: :pa_subelements_mutual_presence?,                  
  validates :country, 
              presence: true, if: :pa_subelements_mutual_presence?,            
              length: { maximum: 50} 

private
 def pa_subelements_mutual_presence? # method to help set validates on mutually dependent for presence for postal address
      lambda { self.address_line_1.present? }  ||
      lambda { self.zipcode.present? }         ||
      lambda { self.state.present? }         ||
      lambda { self.country.present? }
    end
解决方案

It seems to me, it has to be all four or none of them. Not tested, but this should work.

validate :all_or_none

private
    def all_or_none
        errors[:base] << "all or nothing, dude" unless
            (address_line_1.blank? && zipcode.blank? && state.blank? && country.blank?) ||
            (!address_line_1.blank? && !zipcode.blank? && !state.blank? && !country.blank?)
    end

all_or_none will either be true if all four fields are blank or none of them is.

这篇关于轨道4活动记录验证 - 有条件地验证了4个属性presence如果至少有一个是同时允许无以present present的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-24 09:18