0

我已經正確將歐芹應用於其他幾種形式,但現在我遇到了一個將其添加到簡單的Bootstrap 3模式的問題。我有一種感覺,它與將數據 - 香菜排除到字段中然後以某種方式在模式打開時重置它們有關。這裏是我的相關代碼:引導模式的香菜驗證?

<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel"> 
     <div class="modal-dialog" role="document"> 
      <div class="modal-content"> 
       <div class="modal-header"> 
        <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button> 
        <h4 class="modal-title" id="myModalLabel">Become Our Sponsor</h4> 
        <p class='small' style="margin-bottom:0px;">All fields are required.</p> 
       </div> 
       <div class="modal-body"> 
        <form accept-charset="UTF-8" id="become-sponsor-form" method="POST"> 
         <input name="inf_form_xid" type="hidden" value="xxxxxxxx" /> 
         <input name="inf_form_name" type="hidden" value="xxxxxxxx" /> 
         <input name="infusionsoft_version" type="hidden" value="x.xx.x.xx" /> 
         <div class="bs-callout bs-callout-warning invisible mx-auto"> 
          <p>Please fill out all required fields.</p> 
         </div> 
         <div class="form-group"> 
          <label for="inf_field_FirstName">First Name</label> 
          <input class="form-control" id="inf_field_FirstName" name="inf_field_FirstName" type="text" placeholder="Kevin" data-parsley-trigger='change' data-parsley-required data-parsley-excluded/> 
         </div> 
         <div class="form-group"> 
          <label for="inf_field_LastName">Last Name</label> 
          <input class="form-control" id="inf_field_LastName" name="inf_field_LastName" type="text" placeholder='Bacon' data-parsley-trigger='change' data-parsley-required data-parsley-excluded/> 
         </div> 
         <div class="form-group"> 
          <label for="inf_field_Email">Email</label> 
          <input class="form-control" id="inf_field_Email" name="inf_field_Email" type="email" placeholder="[email protected]" data-parsley-type='email' data-parsley-trigger='change' data-parsley-required data-parsley-excluded/> 
         </div> 
         <div class="form-group"> 
          <label for="inf_field_Phone1">Phone</label> 
          <input type="tel" class="form-control" id="inf_field_Phone1" minlength = '10' maxlength="14" data-parsley-error-message='Must be minimum of 10 digits' data-parsley-trigger='change' data-parsley-required data-parsley-excluded> 
          <input id="hidden-number" name='inf_field_Phone1' type="hidden" name="phone-full"> 
         </div> 
         <div class="form-group"> 
          <label for="inf_custom_CompanyType">Type of Business</label> 
          <input class="form-control" id="inf_custom_CompanyType" name="inf_custom_CompanyType" type="text" placeholder='Ecommerce, Marketing Agency, etc.' data-parsley-trigger='change' data-parsley-required data-parsley-excluded/> 
         </div> 
         <input type="submit" id="submit_button" value="Submit" class="btn btn-primary btn-block"/> 
        </form> 
       </div> 
      </div> 
     </div> 
    </div> 

<script type="text/javascript"> 
    $(document).ready(function() { 
     $('#become-sponsor-form').parsley().on('field:validated', function() { 
      var ok = $('.parsley-error').length === 0; 
      if (this.$element.hasClass('parsley-error')) { 
       $('.bs-callout-warning').toggleClass('invisible', ok); 

      }; 
     }); 
    }); 

</script> 

<script type="text/javascript"> 
$(document).ready(function(){ 
    $("#submit_button").click(function(e){ 

      var form_data = $("#become-sponsor-form").serialize(); 
      e.preventDefault(); 

      $("#become-sponsor-form").html('<div class="loading_message text-center"><i class="fa fa-cog fa-spin fa-3x fa-fw"></i></div>'); 
      $.post("formaction.php", form_data, function(result){ 
       $("#become-sponsor-form").html('<div class="success_message alert alert-success text-center" role="alert">Thank You.. We will contact you soon...</div>'); 
      }); 

      } 
     ) 
}) 
</script> 

這在很大程度上是我想表單驗證適用於舊代碼,所以我有一些問題,閱讀的內容已經在那裏了(我是相對較新的)。我確信有一種方法可以將兩個腳本合併爲一個簡潔的腳本,但我還不夠先進。任何幫助將不勝感激!

編輯:忘了實際提及錯誤是什麼。即使我沒有填寫任何字段,表單仍然可以提交。但是每個單獨的表單都正確驗證。 例如:確保電子郵件地址和電話號碼有效,但表單仍在提交,即使是空字段也是如此。

+0

你得到的錯誤是什麼? –

+0

同時擁有'data-parsley-required'和'data-parsley-excluded'是否矛盾是不是? – rayt

+0

@ShalinPatel我在控制檯中沒有收到任何錯誤。儘管如此,即使我沒有填寫任何內容,表單仍然可以提交。我剛剛意識到我並不清楚原始帖子中的問題,對此抱歉! –

回答

0

明白了一切,以防其他人碰到這個問題!

本文來自不同的表單驗證插件幫助了一下:http://formvalidation.io/download/

我最終做的是在頁面加載時,給出輸入data-parsley-excluded。然後在下面的腳本中,添加data-parsley-required並刪除data-parsley-excluded。我還將data-parsley-group應用於輸入以便能夠應用isValid方法。

<script type="text/javascript"> 
    $(document).ready(function() { 
     $('#become-sponsor-btn').on('click', function() { 

      $("#inf_field_FirstName, #inf_field_LastName, #inf_field_Email, #inf_field_Phone1, #inf_custom_CompanyType").attr("required", true); 

      $("#inf_field_FirstName, #inf_field_LastName, #inf_field_Email, #inf_field_Phone1, #inf_custom_CompanyType").removeAttr("data-parsley-excluded"); 

      $('#become-sponsor-form').parsley().on('field:validated', function() { 
       var ok = $('.parsley-error').length === 0; 
       if (this.$element.hasClass('parsley-error')) { 
        $('.bs-callout-warning').toggleClass('invisible', ok); 
       }; 
      }); 
     }); 

     $("#become-sponsor-form").on('submit', function(e){ 
      e.preventDefault(); 
      $('#become-sponsor-form').parsley().on('form:validate', function (formInstance) { 
       var success = formInstance.isValid({group: 'block1'}); 

       console.log(success); 
       if (success) { 
        var form_data = $("#become-sponsor-form").serialize(); 

        $("#become-sponsor-form").html('<div class="loading_message text-center"><i class="fa fa-cog fa-spin fa-3x fa-fw"></i></div>'); 
        $.post("formaction.php", form_data, function(result){ 
         $("#become-sponsor-form").html('<div class="success_message alert alert-success text-center" role="alert">Thank You.. We will contact you soon...</div>'); 
        }); 

       } else { 
        e.preventDefault(); 
        return false;     
       } 

      }); 
     }); 

    }); 


</script>