2016-03-21 74 views
0

我想在驗證表單之前驗證表單,但我始終使用工具提示腳本來驗證表單,但始終將發送到控制器的操作在symfony中進行,儘管驗證不正確。這是我在樹枝在沒有驗證的情況下提交的表單

 <form id="rappel-form" class="form-horizontal" name="rappelform" method="post" enctype="multipart/form-data" 
              action="{{ path('register') }}"> 

.............. 

/> 

,並在我的腳本塊的代碼,這是我的代碼:

<script> 

     jQuery('#formulaire').ajaxForm({ 

     beforeSubmit: function (arr, $form, options) { 


      if(! $form.valid()) return false; 

      else return true; 
     }, 

     success: function (data) { 
      if(data.dataa!=null){ 
       alert(" succées"); 
      } 
      else 
       alert('erreur d éxécution de la requête'); 


     }, 
     error: function() { 
      //jQuery('#main-content').html("erreur d'éxécution de la requête"); 
      alert('erreur d éxécution de la requête'); 

     } 
    }); 
</script> 

和的document.ready

<script type="text/javascript"> 


    $(document).ready(function() { 
     jQuery.validator.addMethod("regexphone", function (value, element, regexp) { 

      if (regexp.constructor != RegExp) 
       regexp = new RegExp(regexp); 
      else if (regexp.global) 
       regexp.lastIndex = 0; 
      return this.optional(element) || regexp.test(value); 
     }, ""); 
     $(document).ready(function() { 
      $('#formulaire input[type="text"]').tooltipster({ 
       trigger: 'custom', // default is 'hover' which is no good here 
       onlyOne: false, // allow multiple tips to be open at a time 
       position: 'right' // display the tips to the right of the element 
      }); 
      $('#formulaire input[type="password"]').tooltipster({ 
       trigger: 'custom', // default is 'hover' which is no good here 
       onlyOne: false, // allow multiple tips to be open at a time 
       position: 'right' // display the tips to the right of the element 
      }); 
      $('#formulaire input[type="number"]').tooltipster({ 
       trigger: 'custom', // default is 'hover' which is no good here 
       onlyOne: false, // allow multiple tips to be open at a time 
       position: 'right' // display the tips to the right of the element 
      }); 

      $('#formulaire').validate({ // initialize the plugin 
       errorPlacement: function (error, element) { 
        $(element).tooltipster('update', $(error).text()); 
        $(element).tooltipster('show'); 
       }, 
       success: function (label, element) { 
        $(element).tooltipster('hide'); 
       }, 
       rules: { 
        'contact[name]': { 
         required: true, 
         minlength: 2 
        }, 
        'contact[gsmPrimary]': { 
         required: true, 
         'regexphone': /^0[1-9][0-9]{8}$/ 
        }, 
        'contact[lastName]': { 
         required: true, 
         minlength: 2 
        }, 
        'contact[listcountry]': { 
         required: true, 

        } 

       }, 
       messages: { 

        'contact[name]': { 
         required: "{{ 'message.contact.nom.required'|trans }}", 
         minlength: "{{ 'message.contact.nom.min'|trans }}", 
         maxlength: "Votre nom doit faire au plus 50 caractères." 
        }, 

        'contact[gsmPrimary]': { 
         required: "{{ 'message.contact.telephone.required'|trans }}", 
         'regexphone': "{{ 'message.contact.telephone.validation'|trans }}" 
        }, 
        'contact[lastName]': { 
         required: "{{ 'message.contact.prenom.required'|trans }}", 
         minlength: "{{ 'message.contact.prenom.min'|trans }}" 
        }, 
        'contact[listcountry]': { 
         required: "{{ 'message.contact.country'|trans }}", 

        } 

       }, 
       submitHandler: function (form) { // for demo 
        //alert('valid form submitted'); // for demo 
        return false; // for demo 
       } 
      }); 

     }); 
    }); 

</script> 

問題,腳本驗證形式和顯示我的錯誤,但之後提交表格給控制器,我遇到了很大的問題。

任何想法,請如何解決這個問題

+0

請說明您的具體問題或添加額外的細節,以確切地突出你所需要的。正如目前所寫,很難確切地說出你在問什麼。請參閱[如何提問](http://stackoverflow.com/help/asking)頁面以獲得澄清此問題的幫助。 – lrnzcig

+0

腳本驗證窗體,顯示錯誤,然後發送窗體,儘管它不正確。通常表單應該是有效的(並且錯誤應該被糾正),然後發送表單,但在我的情況下,腳本在兩種情況下發送表單(正確與否)。和我我需要正確驗證的形式,然後發送表單一次所有的錯誤已被糾正 –

+0

我發現這[相關答案](http://stackoverflow.com/a/11607773/3585500),說'返回假'不起作用,並調用'xhr.abort'。還注意到你的'form id ='rappel-form'',但你的jquery選擇器是'formulaire'。 – ourmandave

回答

0

腳本聽連接到並不存在的元素的事件。假設上面發佈的代碼是正確的,您需要定位要正確提升的表單。

相關問題