Created
August 1, 2010 17:28
-
-
Save rodrigopinto/503552 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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