Search code examples
ruby-on-railsruby-on-rails-3validationfield-with-errors

Rails 3: "field-with-errors" wrapper changes the page appearance. How to avoid this?


Email field:

<label for="job_client_email">Email: </label> 
<input type="email" name="job[client_email]" id="job_client_email">

looks like this:

without_error

But, if the email validation fails, it becomes:

<div class="field_with_errors">
  <label for="job_client_email">Email: </label>
</div> 
<div class="field_with_errors">
  <input type="email" value="wrong email" name="job[client_email]" id="job_client_email">
</div>

which looks like this:

with_error

How could I avoid this appearance change ?


Solution

  • You should override ActionView::Base.field_error_proc. It's currently defined as this within ActionView::Base:

     @@field_error_proc = Proc.new{ |html_tag, instance| 
       "<div class=\"field_with_errors\">#{html_tag}</div>".html_safe
     }
    

    You can override it by putting this in your application's class inside config/application.rb:

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

    Restart rails server for this change to take effect.