Module: ClientSideValidations::ActionView::Helpers::FormHelper

Defined in:
lib/client_side_validations/action_view/form_helper.rb

Defined Under Namespace

Classes: Error

Instance Method Summary collapse

Instance Method Details

#apply_form_for_options!(object_or_array, options) ⇒ Object



39
40
41
42
# File 'lib/client_side_validations/action_view/form_helper.rb', line 39

def apply_form_for_options!(object_or_array, options)
  super
  options[:html][:validate] = true if options[:validate]
end

#fields_for(record_or_name_or_array, *args, &block) ⇒ Object



44
45
46
47
48
# File 'lib/client_side_validations/action_view/form_helper.rb', line 44

def fields_for(record_or_name_or_array, *args, &block)
  output = super
  @validators.merge!(args.last[:validators]) if @validators
  output
end

#form_for(record_or_name_or_array, *args, &proc) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/client_side_validations/action_view/form_helper.rb', line 5

def form_for(record_or_name_or_array, *args, &proc)
  options = args.extract_options!
  if options[:validate]

    content_for_name = options[:validate] unless options[:validate] == true

    # Always turn off HTML5 Validations
    options[:html] ||= {}
    options[:html][:novalidate] = 'novalidate'

    case record_or_name_or_array
    when String, Symbol
      raise ClientSideValidations::ActionView::Helpers::FormHelper::Error, 'Using form_for(:name, @resource) is deprecated in Rails and is not supported with ClientSideValidations. Please use form_for(@resource, :as => :name) instead.'
    when Array
      object = record_or_name_or_array.last
    else
      object = record_or_name_or_array
    end
  end

  @validators = {}
  # Order matters here. Rails mutates the options object
  script = client_side_form_settings(object, options)
  form   = super(record_or_name_or_array, *(args << options), &proc)
  # Because of the load order requirement above this sub is necessary
  # Would be nice to not do this
  script = insert_validators_into_script(script)
  if content_for_name
    content_for(content_for_name) { script.html_safe }
    script = nil
  end
  "#{form}#{script}".html_safe
end