0

客戶端驗證問題我有軌一種形式,是一個多步驟的形式 我已經使用這個寶石 'client_side_validations', github: "DavyJonesLocker/client_side_validations", branch: "4-2-stable"導軌 - 以多步的形式

和形式是

<%= form_for @employee, :html => {:multipart => true, :id => 'msform'}, :validate => true do |f| %> 
     <%= render 'shared/error_messages', :target => @employee %> 
     <ul id='progressbar'> 
     <li class='active'>Account</li> 
     <li>Address</li> 
     <li>Contact</li> 
     </ul> 
     <!-- fieldsets --> 
     <fieldset> 
     <h2 class='fs-title margin_0 bg_red white-text left-align margin_B20'>Create 
      Account</h2> 

     <div class='row form_row padding_L20 padding_R20 margin_B15'> 
      <div class='col s12 m4 left-align'> 
      <label for='usrname' class='form_label'>User Name</label> 
      </div> 
      <%= f.fields_for :profile do |p| %> 
      <div class='col s12 m8'> 
       <div class='input-field'> 
       <%= p.text_field :name, :validate => true, :class => 'validate form_input' %> 
       </div> 
      </div> 
      <% end %> 
     </div> 
     <div class='row form_row padding_L20 padding_R20 margin_B15'> 
      <div class='col s12 m4 left-align'> 
      <label class='form_label'>Email</label> 
      </div> 
      <div class='col s12 m8'> 
      <div class='input-field'> 
       <%= f.text_field :email, :validate => true, :class => 'validate form_input' %> 
      </div> 
      </div> 
     </div> 
     <div class='row form_row padding_L20 padding_R20 margin_B15'> 
      <div class='col s12 m4 left-align'> 
      <label class='form_label'>Role</label> 
      </div> 
      <div class='col s12 m8'> 
      <div class='input-field'> 
       <%= f.select :role, Employee.roles.keys, :selected => @employee[:role], 
          :class => 'browser-default' %> 
      </div> 
      </div> 
     </div> 

      <div class='row form_row padding_L20 padding_R20 margin_B15'> 
      <%= f.fields_for :contact do |c| %> 
       <div class='col s12 m4 left-align'> 
       <label for='phno' class='form_label'>Phone Number</label> 
       </div> 
       <div class='col s12 m8'> 
       <div class='input-field'> 
        <%= c.text_field :phone_no, :class => 'validate form_input' %> 
       </div> 
      <% end %> 
      </div> 
      </div> 
      <input type='button' id="next_form" name='next' class='next action-button' value='Next'/> 
     <% end %> 

</fieldset> 
    <fieldset> 
    <h2 class='fs-title margin_0 bg_red white-text left-align margin_B20'>Create 
     Address</h2> 

    <h3 class='fs-subtitle left-align margin_0'>Current Address</h3> 
    <%= f.fields_for :contact, :validate => true do |c| %> 
     <div class='row form_row padding_L20 padding_R20 margin_B15'> 
     <div class='col s12 m4 left-align'> 
      <label class='form_label'>Address 1</label> 
     </div> 
     <div class='col s12 m8'> 
      <div class='input-field'> 
      <%= c.text_field :current_address1, :class => 'validate form_input', 
          :id=> 'current-address-1', :validate => true %> 
      </div> 
     </div> 
     </div> 
     <div class='row form_row padding_L20 padding_R20 margin_B15'> 
     <div class='col s12 m4 left-align'> 
      <label class='form_label'>Address 2</label> 
     </div> 
     <div class='col s12 m8'> 
      <div class='input-field'> 
      <%= c.text_field :current_address2, :class => 'validate form_input', 
          :id=> 'current-address-2' %> 
      </div> 
     </div> 
     </div> 
     <div class='row form_row padding_L20 padding_R20 margin_B15'> 
     <div class='col s12 m12 left-align'> 
      <div class='row'> 
      <div class='input-field col s12 m6'> 
       <%= c.country_select :country, {prompt: 'Please select a country'}, {:class => 'browser-default'}, {:validate => true} %> 
      </div> 

      <div class='input-field col s12 m6'> 
       <%= render partial: 'subregion_select', locals: {parent_region: c.object.country} %> 
      </div> 
      </div> 
     </div> 
     </div> 

     <div class='row form_row padding_L20 padding_R20 margin_B15'> 
     <div class='col s12 m12 left-align'> 
      <div class='row'> 
      <div class='input-field col s12 m6'> 
       <%= c.text_field :city, :class => 'validate form_input', :placeholder =>'City', 
           :id=>'curr-city', :validate => true %> 
      </div> 
      <div class='input-field col s12 m6'> 
       <%= c.text_field :zip, :class => 'validate form_input', :placeholder =>'Zip Code', 
           :id=>'curr-zip', :validate => true %> 

      </div> 
      </div> 
     </div> 
     </div> 
    <% end %> 

形式id是#msform,下一個按鈕id是#next_form,所以現在我已經在多步驟表單中跟蹤了客戶端驗證,並且我將javascript代碼設置爲

$("#next_form").bind("click", function(e) { 
    //If the form is valid then go to next else don't 
    var valid = true; 
    // this will cycle through all visible inputs and attempt to validate all of them. 
    // if validations fail 'valid' is set to false 
    $('[data-validate] input:visible').each(function() { 
     var settings = window.ClientSideValidations.forms[this.form.id] 
     if (!$(this).isValid(settings.validators)) { 
      valid = false 
     } 
    }); 
    if(valid){ 
     //code to go to next step 
     $("#msform").enableClientSideValidations(); 
    } 
    // if any of the inputs are invalid we want to disrupt the click event 
    return valid; 
}); 

我收到了第一種形式的錯誤消息,但是當我單擊下一步並且其他步驟窗體出現在客戶端驗證不起作用時 我無法解決此問題,因爲3天請幫助!

+0

能否請您進一步擴展,「它不工作」?你實際觀察到什麼?你期望看到什麼?你試圖自行解決這個問題? –

+0

你是否嘗試過從表單的縮減版本(只有一個輸入)開始,然後檢查該工作是否正常......然後添加第二個輸入等等,並查看這個非常簡單的版本中的工作原理和中斷情況?如果你得到這個工作,那麼你可以逐個添加輸入,直到它全部工作。 –

+0

是的,我嘗試了一個輸入,並啓用了這樣的驗證$(「#current-address-1」)。enableClientSideValidations(); 但這不起作用 – user4965201

回答

0

由於您在按鈕事件上使用了'this'變量,因此驗證器可能會發生錯誤。嘗試使用特定形式的選擇是這樣的:

var form = $('#msform'); 
var form_validators = ClientSideValidations.forms[form.attr('id')].validators 
if (!form.isValid(form_validators)) { 
    valid = false; 
} 

也可以嘗試顯示窗體後正在重置驗證:

if(valid){ 
    $("#msform").resetClientSideValidations(); 
    $("#msform").enableClientSideValidations(); 
}