2

Right now I have my ActionView::Base.field_error_proc as

Proc.new do |html_tag, instance|
  if html_tag =~ /^<label/ or instance.respond_to?(:object_name)
    %{<div class="field_with_errors">#{html_tag}</div>}.html_safe
  else
    %{<div class="field_with_errors">#{html_tag}<br /><label for="#{instance.send(:tag_id)}" class="message">#{instance.error_message.first}</label></div>}.html_safe
  end

I had modified this to accomodate a few of my needs when i did use the client_side_validations gems.

Right now, the 'fields_with_error' div wraps around the html_tag(i.e the error field to be specific). What i would like to know though is if its possible to modify the position of the div class="fields_with_error" and place it right after the div that contains the error_field.

For eg,

<div class="main_div">
  <%= password_field_tag 'secret', 'Your secret here' %>
</div>

Then the div class="fields_with_error" should be postioned as

<div class="main_div">
  <%= password_field_tag 'secret', 'Your secret here' %>
</div>
<div class="fields_with_error"> <label class="message"> The error message </label> </div>

Any help would be of great value.

Sunil
  • 3,424
  • 2
  • 24
  • 25

1 Answers1

10

So when I work with twitter bootstrap I usually make an initializer with something like this

ActionView::Base.field_error_proc = Proc.new do |html_tag, instance|
  html = %(<div class="field_with_errors">#{html_tag}</div>).html_safe
  # add nokogiri gem to Gemfile

  form_fields = [
    'textarea',
    'input',
    'select'
  ]

  elements = Nokogiri::HTML::DocumentFragment.parse(html_tag).css "label, " + form_fields.join(', ')

  elements.each do |e|
    if e.node_name.eql? 'label'
      html = %(<div class="control-group error">#{e}</div>).html_safe
    elsif form_fields.include? e.node_name
      if instance.error_message.kind_of?(Array)
        html = %(<div class="control-group error">#{html_tag}<span class="help-inline">&nbsp;#{instance.error_message.uniq.join(', ')}</span></div>).html_safe
      else
        html = %(<div class="control-group error">#{html_tag}<span class="help-inline">&nbsp;#{instance.error_message}</span></div>).html_safe
      end
    end
  end
  html
end

This just adjust regular errors to twitter errors in forms :> so makes everything very nice to look at ;>. You can use same approach.

Cheers if this helps

Jakub Oboza
  • 5,291
  • 1
  • 19
  • 10