在 RefineryCMS 中覆盖 ActionView::Base.field_error_proc [英] Overriding ActionView::Base.field_error_proc in RefineryCMS
问题描述
我正在尝试使用我的精炼CMS 应用中的 client_side_validations gem (https://github.com/bcardarella/client_side_validations) 内联显示客户端验证错误.
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.
当我从无效字段中跳出时,它会按预期包装在 span.fieldWithErrors 标记中,因此我知道 javascript 验证正在工作.但是,即使在覆盖 ActionView::Base.field_error_proc 之后,我也无法显示错误消息.
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 (?):
在 config/initializers/client_side_validations.rb 中:
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
我也尝试使用类似
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??
推荐答案
结果精炼CMS确实覆盖了field_error_proc:
Turns out refineryCMS does indeed override 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
这篇关于在 RefineryCMS 中覆盖 ActionView::Base.field_error_proc的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!