0

我需要对两个字段应用自定义验证,但失败了。它只适用于一个领域。

我在 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.

为什么类别不验证?

4

0 回答 0