Created
October 4, 2009 18:07
-
-
Save dakatsuka/201528 to your computer and use it in GitHub Desktop.
error_message_onカスタマイズ版
This file contains 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
class ActionView::Base | |
@@field_error_proc = Proc.new{ |html_tag, instance| "<span class=\"fieldWithErrors\">#{html_tag}</span>" } | |
end | |
ActionView::Helpers::ActiveRecordHelper.module_eval do | |
def error_message_on(object, method, *args) | |
options = args.extract_options! | |
unless args.empty? | |
ActiveSupport::Deprecation.warn('error_message_on takes an option hash instead of separate ' + | |
'prepend_text, append_text, and css_class arguments', caller) | |
options[:prepend_text] = args[0] || '' | |
options[:append_text] = args[1] || '' | |
options[:css_class] = args[2] || 'formError' | |
end | |
options.reverse_merge!(:prepend_text => '', :append_text => '', :css_class => 'formError') | |
if (obj = (object.respond_to?(:errors) ? object : instance_variable_get("@#{object}"))) && | |
(errors = obj.errors.on(method)) | |
content_tag("span", | |
"#{options[:prepend_text]}#{ERB::Util.html_escape(errors.is_a?(Array) ? errors.first : errors)}#{options[:append_text]}", | |
:class => options[:css_class] | |
) | |
else | |
'' | |
end | |
end | |
def error_message_for_mobile(object, method, *args) | |
options = args.extract_options! | |
unless args.empty? | |
ActiveSupport::Deprecation.warn('error_message_for_mobile takes an option hash instead of separate ' + | |
'prepend_text, append_text, and css_class arguments', caller) | |
options[:prepend_text] = args[0] || '' | |
options[:append_text] = args[1] || '' | |
options[:color] = args[2] || '#ff0000' | |
end | |
options.reverse_merge!(:prepend_text => '', :append_text => '', :color => '#ff0000') | |
if (obj = (object.respond_to?(:errors) ? object : instance_variable_get("@#{object}"))) && | |
(errors = obj.errors.on(method)) | |
html = content_tag("font", | |
"#{options[:prepend_text]}#{ERB::Util.html_escape(errors.is_a?(Array) ? errors.first : errors)}#{options[:append_text]}", | |
:color => options[:color] | |
) | |
html << '<br>' | |
else | |
'' | |
end | |
end | |
end | |
ActionView::Helpers::FormBuilder.class_eval do | |
def error_message_for_mobile(method, *args) | |
@template.error_message_for_mobile(@object, method, *args) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment