简体   繁体   中英

Proc command is not working on checking the inclusion

I have a field called visit_time with two distinct values. They are "AM" and "PM"

I check the presence of the visit_time by the following validation syntax.

validates_presence_of :visit_time,
      message: "visit time is required"

Then I need to check the inclusion validation only if the visit_time is presence, for this I am using the Proc. But it is not working.

validates :visit_time,
        :inclusion  => { :in => [ 'AM', 'PM'],
                         :message    => "%{value} is not a valid time" },
        :if => Proc.new { |o| o.errors.empty? }

Let me know what's wrong on it. Is Proc is not working for inclusion ??? Thanks in advance.

如果您希望包含验证仅在存在时才运行,则应将Proc更改为:

if: Proc.new { |o| o.visit_time.present? }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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