0

我有一个看起来像这样的表单 views/profiles/_form

<div class="field2">
<%= f.label :purchase_date, "Date this horse was purchased" %><br />
<%= f.date_select :purchase_date %>
</div>

<div class="field2">
<%= f.label :owner_name, "Owner Name" %><br />
<%= f.text_field :owner_name %>
 </div>

我在我的模型/profile.rb 中添加了什么验证

验证:purchase_date ??????????

4

2 回答 2

1

您想要排除验证。它看起来像这样:

# Made it a constant, but could also be in a method.
EXCLUSION_DATE = [Date.new(2012,1,1), Date.new(2012,2,1)]
validates :purchase_date, :exclusion => { :in => EXCLUSION_DATES }

您还可以随时添加自己的自定义验证:

validate :check_purchase_date # Name it whatever you want

private
  def check_purchase_date
    exclusion_dates = [Date.new(2012,1,1), Date.new(2012,2,1)]
    if exclusion_dates.include?(self.purchase_date)
      self.errors.add(:purchase_date, "cannot be on a reserved date.")
    end
  end
于 2012-11-27T16:10:41.647 回答
0

例如,为什么不让用户从 jquery datepicker 中进行选择,这样您就可以立即消除 tje 问题?

于 2012-11-27T16:09:08.210 回答