Module: ClientSideValidations::ActionView::Helpers::FormBuilder

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

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.prepended(base) ⇒ Object



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

def self.prepended(base)
  (base.field_helpers - %i[label check_box radio_button fields_for hidden_field file_field]).each do |selector|
    base.class_eval <<-RUBY_EVAL, __FILE__, __LINE__ + 1
      def #{selector}(method, options = {})
        build_validation_options(method, options)
        options.delete(:validate)

        # Cannot call super here, override the whole method
        @template.send(                      #   @template.send(
          #{selector.inspect},               #     "text_field",
          @object_name,                      #     @object_name,
          method,                            #     method,
          objectify_options(options))        #     objectify_options(options))
      end
    RUBY_EVAL
  end
end

Instance Method Details

#check_box(method, options = {}, checked_value = '1', unchecked_value = '0') ⇒ Object



56
57
58
59
60
# File 'lib/client_side_validations/action_view/form_builder.rb', line 56

def check_box(method, options = {}, checked_value = '1', unchecked_value = '0')
  build_validation_options(method, options)
  options.delete(:validate)
  super(method, options, checked_value, unchecked_value)
end

#client_side_form_settings(_options, form_helper) ⇒ Object



25
26
27
28
29
30
31
# File 'lib/client_side_validations/action_view/form_builder.rb', line 25

def client_side_form_settings(_options, form_helper)
  {
    type:      self.class.to_s,
    input_tag: form_helper.class.field_error_proc.call(%(<span id="input_tag" />),  Struct.new(:error_message, :tag_id).new([], '')),
    label_tag: form_helper.class.field_error_proc.call(%(<label id="label_tag" />), Struct.new(:error_message, :tag_id).new([], ''))
  }
end

#collection_select(method, collection, value_method, text_method, options = {}, html_options = {}) ⇒ Object



74
75
76
77
78
# File 'lib/client_side_validations/action_view/form_builder.rb', line 74

def collection_select(method, collection, value_method, text_method, options = {}, html_options = {})
  build_validation_options(method, html_options.merge(name: options[:name]))
  html_options.delete(:validate)
  super(method, collection, value_method, text_method, options, html_options)
end

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



46
47
48
49
50
51
52
53
54
# File 'lib/client_side_validations/action_view/form_builder.rb', line 46

def fields_for(record_name, record_object = nil, fields_options = {}, &block)
  if record_object.is_a?(Hash) && record_object.extractable_options?
    fields_options = record_object
    record_object  = nil
  end

  fields_options[:validate] ||= @options[:validate] if @options[:validate] && !fields_options.key?(:validate)
  super(record_name, record_object, fields_options, &block)
end

#file_field(method, options = {}) ⇒ Object



100
101
102
103
104
# File 'lib/client_side_validations/action_view/form_builder.rb', line 100

def file_field(method, options = {})
  build_validation_options(method, options)
  options.delete(:validate)
  super(method, options)
end

#grouped_collection_select(method, collection, group_method, group_label_method, option_key_method, option_value_method, options = {}, html_options = {}) ⇒ Object



88
89
90
91
92
# File 'lib/client_side_validations/action_view/form_builder.rb', line 88

def grouped_collection_select(method, collection, group_method, group_label_method, option_key_method, option_value_method, options = {}, html_options = {})
  build_validation_options(method, html_options.merge(name: options[:name]))
  html_options.delete(:validate)
  super(method, collection, group_method, group_label_method, option_key_method, option_value_method, options, html_options)
end

#initialize(object_name, object, template, options) ⇒ Object



41
42
43
44
# File 'lib/client_side_validations/action_view/form_builder.rb', line 41

def initialize(object_name, object, template, options)
  super(object_name, object, template, options)
  @options[:validators] = { object => {} }
end

#radio_button(method, tag_value, options = {}) ⇒ Object



62
63
64
65
66
# File 'lib/client_side_validations/action_view/form_builder.rb', line 62

def radio_button(method, tag_value, options = {})
  build_validation_options(method, options)
  options.delete(:validate)
  super(method, tag_value, options)
end

#select(method, choices = nil, options = {}, html_options = {}, &block) ⇒ Object



68
69
70
71
72
# File 'lib/client_side_validations/action_view/form_builder.rb', line 68

def select(method, choices = nil, options = {}, html_options = {}, &block)
  build_validation_options(method, html_options.merge(name: options[:name]))
  html_options.delete(:validate)
  super(method, choices, options, html_options, &block)
end

#time_zone_select(method, priority_zones = nil, options = {}, html_options = {}) ⇒ Object



94
95
96
97
98
# File 'lib/client_side_validations/action_view/form_builder.rb', line 94

def time_zone_select(method, priority_zones = nil, options = {}, html_options = {})
  build_validation_options(method, html_options.merge(name: options[:name]))
  html_options.delete(:validate)
  super(method, priority_zones, options, html_options)
end

#validate(*attrs) ⇒ Object



33
34
35
36
37
38
39
# File 'lib/client_side_validations/action_view/form_builder.rb', line 33

def validate(*attrs)
  options = attrs.pop if attrs.last.is_a?(Hash)
  (attrs.presence || @object._validators.keys).each do |attr|
    build_validation_options(attr, validate: options)
  end
  nil
end