Skip to content

Instantly share code, notes, and snippets.

@rodrigopinto
Created August 1, 2010 17:28
Show Gist options
  • Select an option

  • Save rodrigopinto/503552 to your computer and use it in GitHub Desktop.

Select an option

Save rodrigopinto/503552 to your computer and use it in GitHub Desktop.
require 'hpricot'
class CustomizedErrorFormBuilder < ActionView::Helpers::FormBuilder
def label(method, text = nil, options = {})
text = text || method.to_s.humanize
object = @template.instance_variable_get("@#{@object_name}")
unless object.nil? || options[:hide_errors]
errors = object.errors.on(method.to_sym)
if errors
text += " <font class=\"error\">#{errors.is_a?(Array) ? errors.first : errors}</font>"
end
end
text += " #{options[:additional_text]}" if options[:additional_text]
html_tag = super(method, text, options)
process_field(html_tag)
end
def text_field(method, options = {})
object = @template.instance_variable_get("@#{@object_name}")
error_class = 'fieldWithErrors'
unless object.nil? || options[:hide_errors]
errors = object.errors.on(method.to_sym)
options[:class] = error_class if errors
end
text += " #{options[:additional_text]}" if options[:additional_text]
html_tag = super(method, options)
process_field(html_tag)
end
private
def process_field(html_tag)
html = Hpricot(html_tag)
inner_html = html.search("//div").inner_html
(inner_html.nil? or inner_html.strip.length == 0)? html_tag : inner_html
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment