Module: ClientValidation::FormBuilder

Defined in:
lib/client_validations/form_builder.rb

Instance Method Summary collapse

Instance Method Details

#client_validationsObject



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/client_validations/form_builder.rb', line 3

def client_validations
  if object.new_record?
    form_id = "new_#{object.class.to_s.downcase}"
  else
    form_id = "edit_#{object.class.to_s.downcase}"
  end
  declarations, validations = ClientValidation.current_adapter.render_script(object)
  js = <<-EOF
    #{declarations}
    $('##{form_id}').validate({
      rules: #{validations[:rules].to_json},
      messages: #{validations[:messages].to_json}
    });
  EOF

  html = template.(:script, js, :type => "text/javascript")
  html.respond_to?(:html_safe!) ? html.html_safe! : html
end