2016-03-21 73 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

脚本听连接到并不存在的元素的事件。假设上面发布的代码是正确的,您需要定位要正确提升的表单。

相关问题