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

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

Defined Under Namespace

Classes: Error

Instance Method Summary collapse

Instance Method Details

#apply_csv_form_for_options!(record, object, options) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
# File 'lib/client_side_validations/action_view/form_helper.rb', line 39

def apply_csv_form_for_options!(record, object, options)
  options[:html][:validate] = true if options[:validate]
  options[:html][:method] ||= options[:method]

  # TODO: remove else branch when minimum compatible version when dropping 6.1 support
  if method(:apply_form_for_options!).arity == 2
    apply_form_for_options! object, options
  else
    apply_form_for_options! record, object, options
  end
end

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



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

def fields_for(record_name, record_object = nil, options = {}, &block)
  # Order matters here. Rails mutates the `options` object
  builder = instantiate_builder(record_name, record_object, options)
  output = capture(builder, &block)

  build_bound_validators! options

  output
end

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

Raises:

  • (ArgumentError)


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 10

def form_for(record, options = {}, &block)
  return super unless options[:validate]

  # We are not going to use super here, because we need
  # to inject the csv options in a data attribute in a clean way.
  # So we basically reimplement the whole form_for method
  raise ArgumentError, 'Missing block' unless block

  options[:html] ||= {}

  # Moving the switch statement to another method to
  # lower complexity
  model, object_name = check_record(record, options)

  remote = options.delete(:remote)

  if remote && !embed_authenticity_token_in_remote_forms && options[:authenticity_token].blank?
    options[:authenticity_token] = false
  end

  options[:model] = model
  options[:scope] = object_name
  options[:local] = !remote
  options[:skip_default_ids] = false
  options[:allow_method_names_outside_object] = options.fetch(:allow_method_names_outside_object, false)

  form_with(**options, &block)
end

#form_with(model: nil, scope: nil, url: nil, format: nil, **options, &block) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/client_side_validations/action_view/form_with_helper.rb', line 7

def form_with(model: nil, scope: nil, url: nil, format: nil, **options, &block)
  return super unless options[:validate]

  options[:allow_method_names_outside_object] = true
  options[:skip_default_ids] = false

  url, model, scope = check_model(url, model, format, scope) if model

  if block
    form_tag_with_validators scope, model, options, url, &block
  else
    html_options = html_options_for_form_with(url, model, **options)
    form_tag_html(html_options)
  end
end