override ActionView::Base.field_error_proc since the error class is now on the wrapper

This commit is contained in:
jduff 2010-05-25 09:39:37 +08:00 committed by José Valim
parent 8cb2092c40
commit a7de18f9b7
1 changed files with 24 additions and 1 deletions

View File

@ -10,6 +10,26 @@ module SimpleForm
# end # end
# #
module FormHelper module FormHelper
# based on what is done in formtastic
# http://github.com/justinfrench/formtastic/blob/master/lib/formtastic.rb#L1706
@@default_field_error_proc = nil
# Override the default ActiveRecordHelper behaviour of wrapping the input.
# This gets taken care of semantically by adding an error class to the wrapper tag
# containing the input.
#
FIELD_ERROR_PROC = proc do |html_tag, instance_tag|
html_tag
end
def with_custom_field_error_proc(&block)
@@default_field_error_proc = ::ActionView::Base.field_error_proc
::ActionView::Base.field_error_proc = FIELD_ERROR_PROC
result = yield
::ActionView::Base.field_error_proc = @@default_field_error_proc
result
end
[:form_for, :fields_for, :remote_form_for].each do |helper| [:form_for, :fields_for, :remote_form_for].each do |helper|
class_eval <<-METHOD, __FILE__, __LINE__ class_eval <<-METHOD, __FILE__, __LINE__
def simple_#{helper}(record_or_name_or_array, *args, &block) def simple_#{helper}(record_or_name_or_array, *args, &block)
@ -22,7 +42,10 @@ module SimpleForm
end end
options[:html] ||= {} options[:html] ||= {}
options[:html][:class] = "simple_form \#{css_class} \#{options[:html][:class]}".strip options[:html][:class] = "simple_form \#{css_class} \#{options[:html][:class]}".strip
#{helper}(record_or_name_or_array, *(args << options), &block)
with_custom_field_error_proc do
#{helper}(record_or_name_or_array, *(args << options), &block)
end
end end
METHOD METHOD
end end