【问题标题】:Use value in override error message in Rails 4 for custom Validator在 Rails 4 中为自定义验证器使用覆盖错误消息中的值
【发布时间】:2015-01-01 23:59:40
【问题描述】:

我在 Rails 4.2 上我编写了一个自定义验证器,它将检查输入的值是否存在于另一个表中。我一直在查看其他一些帖子,似乎有特定于上下文或 Rails 版本的首选方式来重用正在验证的value。在rails docs 中,我看到了以下示例:

validates :subdomain, exclusion: { in: %w(www us ca jp),
    message: "%{value} is reserved." }

但是,如果我尝试在自定义消息覆盖中使用 %{value},它不会进行插值,而只会打印“%{value}”。我见过各种称呼“价值”的方式。我也无法从validate_each获得%{value} to work in my Validator definition, but could get #{value} to work (New to ruby, if#{value}?)。

我也一直在努力处理各种格式的验证语句并放入自定义消息。从文档中看起来可以重复的一些事情不是。如果我声明自定义消息的方式导致错误,请告诉我如何更正?

class ExistingGroupValidator < ActiveModel::EachValidator
  def validate_each(record, attribute, value)
    unless Group.where(:code => value).any?
     record.errors[attribute] << (options[:message] || "#{value} is not a valid
group code")
    end
  end
end

class Example < ActiveRecord::Base
  validates :group_code, presence: true
  validates :group_code, :existing_group => {:message => "The code you have enterd ( **what goes here?** ) is not a valid code, please check with your teacher or group
leader for the correct code." }

end

【问题讨论】:

    标签: ruby-on-rails validation ruby-on-rails-4


    【解决方案1】:

    Rails 自动将值放在短语的开头,因此您可以这样做:

    class ExistingGroupValidator < ActiveModel::EachValidator
      def validate_each(record, attribute, value)
        unless Group.where(code: value).any?
         record.errors[attribute] << (options[:message] || 'is not a valid group code')
        end
      end
    end
    
    class Example < ActiveRecord::Base
      validates :group_code, presence: true
      validates :group_code, existing_group: {message: 'is not a valid code, please check with your teacher or group leader for the correct code.' }
    end
    

    另外,请注意,插值始终是"#{1+1}",而不是%

    【讨论】:

    • 这似乎不是我的脚手架命令创建它的默认方式。我的错误消息显示字段名称,然后显示错误,例如使用默认值:Group code is not... 无论如何,我想了解为什么在 Example 类中 #{value} 插值会因 undefined local variable or method value'` 而失败 - 为什么 rails 文档会显示 %{value}
    • 例如,它似乎应该是这个答案中的 %value:stackoverflow.com/a/6073499/689554
    【解决方案2】:

    Rails 使用国际化风格的字符串插值将值添加到消息中。您可以使用 I18n.interpolate 方法来完成此操作。像这样的东西应该可以解决问题:

    class ExistingGroupValidator < ActiveModel::EachValidator
      def validate_each(record, attribute, value)
        unless Group.where(:code => value).any?
         record.errors[attribute] << (I18n.interpolate(options[:message], {value: value}) || "is not a valid group code")
        end
      end
    end
    
    class Example < ActiveRecord::Base
      validates :group_code, presence: true
      validates :group_code, :existing_group => {:message => "The code you have entered, "%{value}", is not a valid code, please check with your teacher or group   leader for the correct code." }
    end
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多