Search code examples
ruby-on-rails-3client-side-validationrefinerycms

Overriding ActionView::Base.field_error_proc in RefineryCMS


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??


Solution

  • 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