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

.included(base) ⇒ Object



4
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
# File 'lib/client_side_validations/action_view/form_builder.rb', line 4

def self.included(base)
  (base.field_helpers.map(&:to_s) - %w(apply_form_for_options! label check_box radio_button fields_for hidden_field)).each do |selector|
    base.class_eval <<-RUBY_EVAL
      def #{selector}_with_client_side_validations(method, options = {})
        build_validation_options(method, options)
        options.delete(:validate)
        #{selector}_without_client_side_validations(method, options)
      end
    RUBY_EVAL

    base.class_eval { alias_method_chain selector, :client_side_validations }
  end

  base.class_eval do
    alias_method_chain :initialize,                :client_side_validations
    alias_method_chain :fields_for,                :client_side_validations
    alias_method_chain :check_box,                 :client_side_validations
    alias_method_chain :radio_button,              :client_side_validations
    alias_method_chain :select,                    :client_side_validations
    alias_method_chain :collection_select,         :client_side_validations
    alias_method_chain :grouped_collection_select, :client_side_validations
    alias_method_chain :time_zone_select,          :client_side_validations

    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
  end
end

Instance Method Details

#check_box_with_client_side_validations(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_with_client_side_validations(method, options = {}, checked_value = "1", unchecked_value = "0")
  build_validation_options(method, options)
  options.delete(:validate)
  check_box_without_client_side_validations(method, options, checked_value, unchecked_value)
end

#collection_select_with_client_side_validations(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_with_client_side_validations(method, collection, value_method, text_method, options = {}, html_options = {})
  build_validation_options(method, html_options.merge(:name => options[:name]))
  html_options.delete(:validate)
  collection_select_without_client_side_validations(method, collection, value_method, text_method, options, html_options)
end

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



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

def fields_for_with_client_side_validations(record_or_name_or_array, *args, &block)
  options = args.extract_options!
  options[:validate] ||= @options[:validate] if @options[:validate] && !options.key?(:validate)
  fields_for_without_client_side_validations(record_or_name_or_array, *(args << options), &block)
end

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



80
81
82
83
84
# File 'lib/client_side_validations/action_view/form_builder.rb', line 80

def grouped_collection_select_with_client_side_validations(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)
  grouped_collection_select_without_client_side_validations(method, collection, group_method, group_label_method, option_key_method, option_value_method, options, html_options)
end

#initialize_with_client_side_validations(object_name, object, template, options, proc) ⇒ Object



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

def initialize_with_client_side_validations(object_name, object, template, options, proc)
  initialize_without_client_side_validations(object_name, object, template, options, proc)
  @options[:validators] = { object => {} }
end

#radio_button_with_client_side_validations(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_with_client_side_validations(method, tag_value, options = {})
  build_validation_options(method, options)
  options.delete(:validate)
  radio_button_without_client_side_validations(method, tag_value, options)
end

#select_with_client_side_validations(method, choices, options = {}, html_options = {}) ⇒ Object



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

def select_with_client_side_validations(method, choices, options = {}, html_options = {})
  build_validation_options(method, html_options.merge(:name => options[:name]))
  html_options.delete(:validate)
  select_without_client_side_validations(method, choices, options, html_options)
end

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



86
87
88
89
90
# File 'lib/client_side_validations/action_view/form_builder.rb', line 86

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

#validate(*attrs) ⇒ Object



37
38
39
40
41
42
43
# File 'lib/client_side_validations/action_view/form_builder.rb', line 37

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