Class: ActionView::Helpers::FormBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/jquery_validator/form_builder.rb

Instance Method Summary collapse

Instance Method Details

#jquery_validators(options = {}) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/jquery_validator/form_builder.rb', line 4

def jquery_validators(options = {})
    fields = options.delete(:only)
    
    @object.class.validators.each do |v|
      if !fields || fields.include?(v.attributes.first)
        if validator = JqueryValidator.factory(v, self)
          options.deep_merge!(JqueryValidator.factory(v, self).validate_arguments)
        end
      end
    end
    js = options.to_json
    js.gsub!(/"\s*(function\(.*\})\s*\"/, '\1')
    code = %Q[
      <script>
      $(document).ready(function() {
        $("##{ActionController::RecordIdentifier.dom_id(@object, @object.new_record? ? :new : :edit)}").validate(#{js});
      });
      </script>
    ]
    code.html_safe
end