验证多列的allow

系统教程 行业动态 更新时间:2024-06-14 17:04:02
验证多列的allow_blank(Validates allow_blank of multiple columns)

我根据其active_state验证Realty对象,因此如果它处于挂起状态 ,则允许多个字段为blank 。

with_options :if => Proc.new { |a| a.active_state == 'pending'} do |realty| realty.validates :street, :length => {:in => 1..100}, :allow_blank => true realty.validates :postalcode, :numericality => {:only_integer => true}, :length => {:in => 4..5}, :allow_blank => true realty.validates :city, :length => {:in => 1..50}, :allow_blank => true realty.validates :description, :length => {:maximum => 8000}, :allow_blank => true realty.validates :leasing_costs, :numericality => {:only_integer => true}, :length => {:in => 1..9}, :allow_blank => true end with_options :if => Proc.new { |a| a.active_state != 'pending'} do |realty| realty.validates :street, :length => {:in => 1..100} realty.validates :postalcode, :numericality => {:only_integer => true}, :length => {:in => 4..5} realty.validates :city, :length => {:in => 1..50} realty.validates :description, :length => {:maximum => 8000} realty.validates :leasing_costs, :numericality => {:only_integer => true}, :length => {:in => 1..9} end

唯一的区别是:allow_blank => true选项。

我想让这段代码更干,所以我的尝试是使用普通的验证块一次:

validates :street, :length => {:in => 1..100} validates :postalcode, :numericality => {:only_integer => true}, :length => {:in => 4..5} validates :city, :length => {:in => 1..50} validates :description, :length => {:maximum => 8000} validates :leasing_costs, :numericality => {:only_integer => true}, :length => {:in => 1..9}

然后在状态挂起的情况下简单地在所有这些字段上调用一些函数:

with_options :if => Proc.new { |a| a.active_state == 'pending'} do |realty| realty.allow_blank_of :street, :postalcode, :city, :description, :leasing_costs end

类似于所有那些validates_uniqueness_of :x, :y, :z方法。

我找不到适合我需要的功能。 我该如何处理?

I validate Realty objects depending on their active_state, so if it's pending, multiple fields are allowed to be blank.

with_options :if => Proc.new { |a| a.active_state == 'pending'} do |realty| realty.validates :street, :length => {:in => 1..100}, :allow_blank => true realty.validates :postalcode, :numericality => {:only_integer => true}, :length => {:in => 4..5}, :allow_blank => true realty.validates :city, :length => {:in => 1..50}, :allow_blank => true realty.validates :description, :length => {:maximum => 8000}, :allow_blank => true realty.validates :leasing_costs, :numericality => {:only_integer => true}, :length => {:in => 1..9}, :allow_blank => true end with_options :if => Proc.new { |a| a.active_state != 'pending'} do |realty| realty.validates :street, :length => {:in => 1..100} realty.validates :postalcode, :numericality => {:only_integer => true}, :length => {:in => 4..5} realty.validates :city, :length => {:in => 1..50} realty.validates :description, :length => {:maximum => 8000} realty.validates :leasing_costs, :numericality => {:only_integer => true}, :length => {:in => 1..9} end

The only difference are those :allow_blank => true options.

I want to make this code more dry, so my attempt would be to use the normal validations block once:

validates :street, :length => {:in => 1..100} validates :postalcode, :numericality => {:only_integer => true}, :length => {:in => 4..5} validates :city, :length => {:in => 1..50} validates :description, :length => {:maximum => 8000} validates :leasing_costs, :numericality => {:only_integer => true}, :length => {:in => 1..9}

and then simply call some function on all of those fields in case the state is pending:

with_options :if => Proc.new { |a| a.active_state == 'pending'} do |realty| realty.allow_blank_of :street, :postalcode, :city, :description, :leasing_costs end

Similar to all those validates_uniqueness_of :x, :y, :z methods.

I couldn't find a function to fit my need. How can I approach this?

最满意答案

如果:allow_blank选项采用了proc,那将是很好的,但我认为它没有。 但是,您可以使用:if选项获得相同的结果,因为:allow_blank本质上是一种说法“如果属性为空,则不运行此验证要求”。 所以试试这个:

with_options :if => Proc.new { |a| a.active_state == 'pending' ? a.present? : true } do |realty| realty.validates :street, :length => {:in => 1..100} realty.validates :postalcode, :numericality => {:only_integer => true}, :length => {:in => 4..5} realty.validates :city, :length => {:in => 1..50} realty.validates :description, :length => {:maximum => 8000} realty.validates :leasing_costs, :numericality => {:only_integer => true}, :length => {:in => 1..9} end

在这种情况下,proc表示......如果active_state为'pending'且属性存在,则执行此验证。 但是,如果active_state不是'pending'则不允许该属性为空,因此始终运行验证。 我希望我能根据你的需要得到正确的逻辑。

From pdobb's answer

However, you can achieve the same result using the :if option since :allow_blank is essentially a way of saying "if the attribute is blank then don't run this validation requirement".

Based on pdobbs explanation what allow_blank => true actually does, I could narrow the validations down to:

validates :street, :length => {:in => 1..100}, :unless => :pending? validates :postalcode, :numericality => {:only_integer => true}, :length => {:in => 4..5}, :unless => :pending? validates :city, :length => {:in => 1..50}, :unless => :pending? validates :description, :length => {:maximum => 8000}, :unless => :pending? validates :leasing_costs, :numericality => {:only_integer => true}, :length => {:in => 1..9}, :unless => :pending? def pending? active_state == "pending" end

So I simply skip all of those validations if the active_state is "pending".

Allot more DRY :)

更多推荐

本文发布于:2023-04-24 20:56:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/dzcp/e21492e2be39741e20db5ae815d08edb.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!