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!(record, object, options) ⇒ Object



60
61
62
63
# File 'lib/client_side_validations/action_view/form_helper.rb', line 60

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

#assign_script_to_content_for(name, script) ⇒ Object



51
52
53
54
55
56
57
58
# File 'lib/client_side_validations/action_view/form_helper.rb', line 51

def assign_script_to_content_for(name, script)
  # rubocop:disable OutputSafety
  # TODO: check if html_safe is really needed here
  return unless name && name != true
  content_for name, script.html_safe
  true
  # rubocop:enable OutputSafety
end

#fields_for(record_or_name_or_array, record_object = nil, options = {}, &block) ⇒ Object



65
66
67
68
69
# File 'lib/client_side_validations/action_view/form_helper.rb', line 65

def fields_for(record_or_name_or_array, record_object = nil, options = {}, &block)
  output = super
  build_bound_validators options
  output
end

#form_for(record, options = {}, &block) ⇒ Object

Raises:

  • (ArgumentError)


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
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/client_side_validations/action_view/form_helper.rb', line 9

def form_for(record, options = {}, &block)
  raise ArgumentError, 'Missing block' unless block_given?
  if options[:validate]

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

    case record
    when String, Symbol
      raise ClientSideValidations::ActionView::Helpers::FormHelper::Error, 'Using form_for(:name, @resource) is not supported with ClientSideValidations. Please use form_for(@resource, as: :name) instead.'
    else
      object = record.is_a?(Array) ? record.last : record
      object_name = options[:as] || model_name_from_record_or_class(object).param_key
    end
  end

  @validators = {}

  # Order matters here. Rails mutates the options object
  html_id = options[:html][:id] if options[:html]
  form = super(record, options, &block)
  options[:id] = html_id if html_id

  build_bound_validators options
  builder = instantiate_builder(object_name, object, options) if object_name && object
  script = client_side_form_settings(object, options, builder)

  # Because of the load order requirement above this sub is necessary
  # Would be nice to not do this
  script = insert_validators_into_script(script)

  # rubocop:disable OutputSafety
  # TODO: check if html_safe is really needed here
  if assign_script_to_content_for(options[:validate], script)
    form.html_safe
  else
    "#{form}#{script}".html_safe
  end
  # rubocop:enable OutputSafety
end