2

我正在尝试使用我的refineryCMS 应用程序中的client_side_validations gem(https://github.com/bcardarella/client_side_validations)在线显示客户端验证错误。

当我跳出一个无效字段时,它会像预期的那样被包裹在一个 span.fieldWithErrors 标记中,所以我知道 javascript 验证正在工作。但是,即使在覆盖 ActionView::Base.field_error_proc 之后,我也无法显示错误消息。

我有一种感觉,我的初始化程序随后被炼油厂(?)覆盖:

在 config/initializers/client_side_validations.rb 中:

# Uncomment the following block if you want each input field to have the validation messages attached.
ActionView::Base.field_error_proc = Proc.new do |html_tag, instance|
  unless html_tag =~ /^<label/
    %{<div class="field_with_errors">#{html_tag}<label for="#{instance.send(:tag_id)}" class="message">#{instance.error_message.first}</label></div>}.html_safe
  else
    %{<div class="field_with_errors">#{html_tag}</div>}.html_safe
  end
end

我还尝试使用类似的东西从 config/application.rb 设置 field_error_proc

config.action_view.field_error_proc = Proc.new { |html_tag, instance| # etc... }  

这些似乎都对渲染无效字段没有任何影响。有任何想法吗??

4

1 回答 1

3

事实证明,refineryCMS 确实覆盖了 field_error_proc:

https://github.com/refinery/refinerycms/issues/961

这对我有用:

# Uncomment the following block if you want each input field to have the validation messages attached.
Rails::Application.refinery.after_inclusion do
  ActionView::Base.field_error_proc = Proc.new do |html_tag, instance|
    unless html_tag =~ /^<label/
      %{<div class="field_with_errors">#{html_tag}<label for="#{instance.send(:tag_id)}" class="message">#{instance.error_message.first}</label></div>}.html_safe
    else
      %{<div class="field_with_errors">#{html_tag}</div>}.html_safe
    end
  end
end
于 2011-11-05T22:10:28.437 回答