我需要为两个字段应用自定义验证,但失败了。它仅适用于一个字段。
我在lib文件夹中创建了文件select_option_validator.rb,这里是代码:
class SelectOptionValidator < ActiveModel::EachValidator
def validate_each(object, attribute, value)
if value == "--- please select ---"
object.errors[attribute] << (options[:message] || "should be selected")
end
end
end
以下是我在模型中的验证:
validates :category, :select_option => true
validates :language, :select_option => true
我的观点:
<% if @website.errors.any? %>
<div class="alert alert-error">
<%= pluralize(@website.errors.count, "error") %> prohibited
this website from being saved:
</div>
<div class="error_explanation">
<ul>
<% @website.errors.full_messages.each do |msg| %>
<li>* <%= msg %></li>
<% end %>
</ul>
</div>
但它只给我
* Language should be selected
但也应该
* Category should be selected.
为什么类别没有验证?