Sha256: 78f7ec0eac3f6daaa22dafd616de17bfc9891ec315713502c17ae5211960bf84

Contents?: true

Size: 1.13 KB

Versions: 3

Compression:

Stored size: 1.13 KB

Contents

module EffectiveFormBuilderHelper
  def effective_form_with(**options, &block)
    options[:class] = [options[:class], 'needs-validation', ('form-inline' if options[:layout] == :inline)].compact.join(' ')
    options[:html] = (options[:html] || {}).merge(novalidate: true, onsubmit: 'return EffectiveBootstrap.validate(this);')

    if options.delete(:remote) == true
      @_effective_remote_index ||= 0

      if options[:html][:data].kind_of?(Hash)
        options[:html][:data][:remote] = true
        options[:html][:data]['data-remote-index'] = (@_effective_remote_index += 1)
      else
        options[:html]['data-remote'] = true
        options[:html]['data-remote-index'] = (@_effective_remote_index += 1)
      end
    end

    without_error_proc do
      form_with(**options.merge(builder: Effective::FormBuilder), &block)
    end
  end

  private

  # Disables a .fields_with_errors wrapping div when
  def without_error_proc
    original = ActionView::Base.field_error_proc

    begin
      ActionView::Base.field_error_proc = proc { |input, _| input }; yield
    ensure
      ActionView::Base.field_error_proc = original
    end
  end

end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
effective_bootstrap-0.0.10 app/helpers/effective_form_builder_helper.rb
effective_bootstrap-0.0.9 app/helpers/effective_form_builder_helper.rb
effective_bootstrap-0.0.8 app/helpers/effective_form_builder_helper.rb