Search code examples
ajaxpartialclient-side-validation

Using ClientSideValidations gem inside ajax rendered partial


I am currently using the ClientSideValidations gem and stuck while rendering a partial using ajax and trying to validate addresses inside that rendered partial with the gem mentioned above. Nothing happens when entering a wrong combination specified in the model.

If browsing to the address-form directly and trying out validations, everything works fine, just like specified.

Any hints or thoughts on how to make the validations gonna work inside the partial?

Thanks!

EDIT: No errors in JS console, just nothing happens when for example entering a too short zipcode (specified in the model with 5 digits). Btw I use haml for the views.

So the code in my view:

 = link_to "Shipping", addresses_path, :remote => true

corresponding controller addresses_controller.rb

respond_to do |format|
   ... 
   format.js {render :layout => false}
   ...
end

corresponding index.js.erb

$("#ajax_content").html("<%= escape_javascript(render :partial =>     
"partialXY") %>");

and corresponding partial

 = form_for @address, :validate => true, :id => "address_form", :remote => true do |f|
   - if @address.errors.any?
     #error_explanation
     %h2
        = pluralize(@address.errors.count, "error")
        prohibited this user from being saved:
        %ul
         - @address.errors.full_messages.each do |msg|
          %li
          =msg
  %ul
    %li
      = f.label :type
      = f.select :address_type, [['Billing Address', 'billing'],['Shipping Address',
      'shipping']], :class => "address_selection"
    %li
      = f.label :gender
      = f.select :gender, [['Male', 'male'],['Female', 'female']], :class => "text_field"
    %li
      = f.label :last_name
      = f.text_field :last_name, :id => "last_name", :class => "text_field"
    %li
      = f.label :first_name
      = f.text_field :first_name, :class => "text_field"
    %li
      = f.label :company_name
      = f.text_field :company_name, :class => "text_field"
    %li
      = f.label :street
      = f.text_field :street, :class => "text_field"
    %li
      = f.label :number
      = f.text_field :number, :class => "text_field"
    %li
      = f.label :zipcode
      = f.text_field :zipcode, :class => "text_field"
    %li
      = f.label :place
      = f.text_field :place, :class => "text_field"
    %li
      = f.label :phone_no
      = f.text_field :phone_no, :class => "text_field"
    %li
      = f.label :country
      = f.text_field :country, :class => "text_field"
    %li
      = f.label :email
      = f.text_field :email, :class => "text_field"
    %li
      = f.submit

so like I said nothing happens when validating the rendered partial inputs like zipcode, etc. The funny thing is, that if you look at the following, automatically by rails generated view for editing addresses, the validation works just fine.

rails generated view to edit address

=render 'partialXY'

I have been working on this issue for a long time and have absolutely no clue on how to fix this. I'm sure it has something to do with ajax since using validation when rendering the rails generated partial works just fine.

Thanks a lot! Phil


Solution

  • Ok I fixed it. Turned out despite giving the form an ID, the ID was a different one in the final html code. So I just added a

    $('form.form_real_id').validate();
    

    to my .js.erb file!