如何选择验证单选按钮?

时间:2016-03-08 21:52:39

标签: ruby-on-rails-4 activeadmin formtastic

我正在尝试验证" Never"单选按钮被选中,或者"日期" (使用 datepicker )选择日期,因此:expiry_date

:expiresstring type

:expiry_datedate type

      f.input :expires, as: :radio, :collection => ["Never", "Date"]
      f.input :expiry_date, as: :string, :wrapper_html => { :class => "date_picker"}

到目前为止,这是我的验证。

  validates :expires,
  inclusion: { in: ["Never", "Date"],
  presence: { message: "none selected"}}
  validates :expiry_date, presence: true

1 个答案:

答案 0 :(得分:0)

您可以使用custom validation

class Food < ActiveRecord::Base
  validate :has_expiry_date

  def has_expiry_date
    if expires == "Date" && !expiry_date.present?
      errors.add(:expiry_date, 'needs to be present if food expires')
    end
  end
end