Skip to content
This repository
Browse code

allows custom field_error_proc

  • Loading branch information...
commit 514f1582d3fbec7f151215985fd521ef45a76ed0 1 parent 521d894
Arnau Sanchez tokland authored
16 lib/formtastic/helpers/form_helper.rb
@@ -58,6 +58,12 @@ module FormHelper
58 58 @@default_form_class = 'formtastic'
59 59 mattr_accessor :default_form_class
60 60
  61 + # Allows to set a custom field_error_proc wrapper. By default this wrapper
  62 + # is disabled since `formtastic` already adds an error class to the LI tag
  63 + # containing the input. Change this from `config/initializers/formtastic.rb`.
  64 + @@field_error_proc = proc { |html_tag, instance_tag| html_tag }
  65 + mattr_accessor :field_error_proc
  66 +
61 67 # Wrapper around Rails' own `form_for` helper to set the `:builder` option to
62 68 # `Formtastic::FormBuilder` and to set some class names on the `<form>` tag such as
63 69 # `formtastic` and the downcased and underscored model name (eg `post`).
@@ -178,17 +184,9 @@ def semantic_fields_for(record_name, record_object = nil, options = {}, &block)
178 184
179 185 protected
180 186
181   - # Override the default ActiveRecordHelper behaviour of wrapping the input.
182   - # This gets taken care of semantically by adding an error class to the LI tag
183   - # containing the input.
184   - # @private
185   - FIELD_ERROR_PROC = proc do |html_tag, instance_tag|
186   - html_tag
187   - end
188   -
189 187 def with_custom_field_error_proc(&block)
190 188 default_field_error_proc = ::ActionView::Base.field_error_proc
191   - ::ActionView::Base.field_error_proc = FIELD_ERROR_PROC
  189 + ::ActionView::Base.field_error_proc = @@field_error_proc
192 190 yield
193 191 ensure
194 192 ::ActionView::Base.field_error_proc = default_field_error_proc
23 spec/helpers/form_helper_spec.rb
@@ -135,6 +135,29 @@
135 135 end
136 136 end
137 137
  138 + describe ActionView::Base.field_error_proc do
  139 + it 'is set to no-op wrapper by default' do
  140 + semantic_form_for(@new_post, :url => '/hello') do |builder|
  141 + ::ActionView::Base.field_error_proc.call("html").should == "html"
  142 + end
  143 + end
  144 +
  145 + it 'is set to the configured custom field_error_proc' do
  146 + field_error_proc = mock()
  147 + Formtastic::Helpers::FormHelper.field_error_proc = field_error_proc
  148 + semantic_form_for(@new_post, :url => '/hello') do |builder|
  149 + ::ActionView::Base.field_error_proc.should == field_error_proc
  150 + end
  151 + end
  152 +
  153 + it 'is restored to its original value after the form is rendered' do
  154 + lambda do
  155 + Formtastic::Helpers::FormHelper.field_error_proc = proc {}
  156 + semantic_form_for(@new_post, :url => '/hello') { |builder| }
  157 + end.should_not change(::ActionView::Base, :field_error_proc)
  158 + end
  159 + end
  160 +
138 161 describe "with :builder option" do
139 162 it "yields an instance of the given builder" do
140 163 class MyAwesomeCustomBuilder < Formtastic::FormBuilder

0 comments on commit 514f158

Please sign in to comment.
Something went wrong with that request. Please try again.