在 RefineryCMS 中重写 ActionView::Base.field_error_proc

发布于 2024-12-13 15:24:15 字数 1074 浏览 3 评论 0原文

我试图在我的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... }  

我还尝试使用以下 对渲染无效字段有任何影响。有什么想法吗?

I'm trying to have client-side validation errors show up inline, using the client_side_validations gem (https://github.com/bcardarella/client_side_validations) in my refineryCMS app.

When I tab out of an invalid field, it gets wrapped in a span.fieldWithErrors tag, as expected, so I know that the javascript validations are working. However, I am unable to have the error messages show up, even after overriding ActionView::Base.field_error_proc.

I have a feeling that my initializer is being subsequently overridden by refinery (?):

In 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

I have also tried setting the field_error_proc from config/application.rb using something along the lines of

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

Neither of these seem to have any effect on the rendering invalid fields. Any ideas??

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

波浪屿的海角声 2024-12-20 15:24:15

事实证明,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

Turns out refineryCMS does indeed override field_error_proc:

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

This worked for me:

# 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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文