Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails wraps fields with errors by div

How can I stop Rails to change my code when validation isn't passed.

Every time rails wraps my field with

<div class='field_with_error'>...</div>

I can edit fields_with_error class

.fields_with_error{ display: inline }

which works, but it is hacky

like image 492
fl00r Avatar asked Mar 18 '11 16:03

fl00r


2 Answers

Its fine. Use the CSS thing instead of doing this.

ActionView::Base.field_error_proc = Proc.new do |html_tag, instance_tag|
  "<span class='field_error'>#{html_tag}</span>"
end

Which I feel is more hacky :)

like image 132
Rishav Rastogi Avatar answered Sep 18 '22 03:09

Rishav Rastogi


I use this in environment.rb. Even more hacky ;-)

#
# Fix annoying <div class="fieldsWithError"> wrapping after validation
# http://dev.rubyonrails.org/ticket/3587
#

ActionView::Base.field_error_proc = Proc.new { |html_tag, instance| 
  msg = instance.error_message 

  if html_tag =~ /<(input|textarea|select)[>]+class=/
    class_attribute = html_tag =~ /class=['"]/ 
    html_tag.insert(class_attribute + 7, "error ") 
  elsif html_tag =~ /<(input|textarea|select)/
    first_whitespace = html_tag =~ /\s/ 
    html_tag[first_whitespace] = " class='error' "
  end 

  html_tag
}
like image 36
auralbee Avatar answered Sep 21 '22 03:09

auralbee