1. <tfoot id='tJ68P'></tfoot>
      <legend id='tJ68P'><style id='tJ68P'><dir id='tJ68P'><q id='tJ68P'></q></dir></style></legend>

        <bdo id='tJ68P'></bdo><ul id='tJ68P'></ul>
    2. <i id='tJ68P'><tr id='tJ68P'><dt id='tJ68P'><q id='tJ68P'><span id='tJ68P'><b id='tJ68P'><form id='tJ68P'><ins id='tJ68P'></ins><ul id='tJ68P'></ul><sub id='tJ68P'></sub></form><legend id='tJ68P'></legend><bdo id='tJ68P'><pre id='tJ68P'><center id='tJ68P'></center></pre></bdo></b><th id='tJ68P'></th></span></q></dt></tr></i><div id='tJ68P'><tfoot id='tJ68P'></tfoot><dl id='tJ68P'><fieldset id='tJ68P'></fieldset></dl></div>

      <small id='tJ68P'></small><noframes id='tJ68P'>

        Yii - ajax 加载表单元素的用户端验证

        时间:2023-09-24
          <bdo id='BHU70'></bdo><ul id='BHU70'></ul>

              <tbody id='BHU70'></tbody>

                  <tfoot id='BHU70'></tfoot>
                  <legend id='BHU70'><style id='BHU70'><dir id='BHU70'><q id='BHU70'></q></dir></style></legend>
                  <i id='BHU70'><tr id='BHU70'><dt id='BHU70'><q id='BHU70'><span id='BHU70'><b id='BHU70'><form id='BHU70'><ins id='BHU70'></ins><ul id='BHU70'></ul><sub id='BHU70'></sub></form><legend id='BHU70'></legend><bdo id='BHU70'><pre id='BHU70'><center id='BHU70'></center></pre></bdo></b><th id='BHU70'></th></span></q></dt></tr></i><div id='BHU70'><tfoot id='BHU70'></tfoot><dl id='BHU70'><fieldset id='BHU70'></fieldset></dl></div>
                1. <small id='BHU70'></small><noframes id='BHU70'>

                2. 本文介绍了Yii - ajax 加载表单元素的用户端验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我在静态表单中使用 Yii 用户端验证,它很棒.但我不知道如何为 ajax 加载的元素添加验证器.

                  I'm using Yii user side validation in static forms and it's great. But I don't know how to add validators for ajax loaded elements.

                  我有一个简单的表单小部件,我想通过 AJAX 将更多的输入字段加载到其中(小 jQuery 脚本没有问题).但我不知道如何为加载的元素添加 Yii javascript 验证器 - 我的意思是自动创建的 JS 验证器,如:

                  I have simple form widget and I would like to load few more input fields into it via AJAX (that's not problem with small jQuery script). But I don't know how to add Yii javascript validators for loaded elements - I mean auto created JS validators like:

                  <script type="text/javascript">
                  /*<![CDATA[*/
                  jQuery(function($) {
                  $('#newsletter-form-footer').yiiactiveform({'validateOnSubmit':true,'validateOnChange':false,'afterValidate':Form.handleByAjax,'attributes':[{'id':'NewsletterForm_emailaddress','inputID':'NewsletterForm_emailaddress','errorID':'NewsletterForm_emailaddress_em_','model':'NewsletterForm','name':'emailaddress','enableAjaxValidation':false,'clientValidation':function(value, messages, attribute) {
                  
                  if($.trim(value)=='') {
                      messages.push("  VALIDATOR_REQUIRED");
                  }
                  
                  
                  if($.trim(value)!='' && !value.match(/^[a-zA-Z0-9!#$%&'*+/=?^_`{|}~-]+(?:.[a-zA-Z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-zA-Z0-9](?:[a-zA-Z0-9-]*[a-zA-Z0-9])?.)+[a-zA-Z0-9](?:[a-zA-Z0-9-]*[a-zA-Z0-9])?$/)) {
                      messages.push("  VALIDATOR_EMAIL");
                  }
                  
                  }}]});
                  });
                  /*]]>*/
                  </script>
                  

                  有什么方法可以添加或删除验证器吗?

                  Is there any way how to add or remove that validators?

                  推荐答案

                  这有点棘手...如果你想用 yiiactiveform 来做,你必须:

                  This is little bit tricky... If you want to do it using yiiactiveform, you have to:

                  1. 向验证添加字段,或
                  2. 从验证中删除字段

                  我的建议是:

                  1. 创建您自己的 JavaScript 验证函数(您可能想在此处复制 yii 的验证代码)

                  1. Create your own JavaScript validation functions (you may want to copy yii's validation code here)

                  $('#newsletter-form-footer').submit(function() {
                          return MyValidator.validate();
                  });
                  

                3. 当您将字段加载到表单中时

                4. When you load a field into the form

                  //You have to get rulesORoptions
                  // along with fieldName from your ajax request.
                  MyValidator.add(fieldName, rulesORoptions);
                  

                5. 当您从表单中删除字段时

                6. When you Remove a field out of the form

                  MyValidator.remove(fieldName);
                  

                7. 此处为 MyValidator 代码...

                8. MyValidator code here...

                  var MyValidator = {
                          fields: {},
                  
                          add: function(fieldName, rulesORoptions) {
                                  this.fields[fieldName] = rulesORoptions;
                          },
                          remove: function(fieldName) {
                                  delete this.fields[fieldName];
                          },
                  
                          validate: function() {
                                  var success = true;
                                  for(var fieldName in this.fields) {
                                          for(var rule in this.fields[fieldName]) {
                  
                                                  if( eval('MyValidator.'+rule+'($("#'+fieldName+'").val())') == false ) {
                                                          $("#'+fieldName+'_em_").html("validation failed (some error text, rulesORoptions may contain error text )");
                                                          success = false;
                                                  }
                                          }
                                  }
                  
                                  return success;
                          },
                  
                          /* Validation methods are here, 
                             copy the javascript validation code from Yii
                          */
                          email: function(value) {
                                  if($.trim(value)!='' && !value.match(/^[a-zA-Z0-9!#$%&'*+/=?^_`{|}~-]+(?:.[a-zA-Z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-zA-Z0-9](?:[a-zA-Z0-9-]*[a-zA-Z0-9])?.)+[a-zA-Z0-9](?:[a-zA-Z0-9-]*[a-zA-Z0-9])?$/)) {
                                          return false;
                                  }
                  
                                  return true;
                          },
                          required: function(value) {
                                  if($.trim(value)=='') {
                                          return false;
                                  }
                  
                                  return true;
                          },
                          number: function(value) {
                                  if(/*copy yii validation code */) {
                                          return false;
                                  }
                  
                                  return true;
                          },
                          .....
                          .....
                  };
                  

                9. 这篇关于Yii - ajax 加载表单元素的用户端验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:用 AJAX 加载的 CKEditor + Yii:$_POST 不包含更新的值 下一篇:CDbConnection 打开数据库连接失败:在 Yii 中找不到驱动程序

                  相关文章

                10. <small id='Ee2x2'></small><noframes id='Ee2x2'>

                  • <bdo id='Ee2x2'></bdo><ul id='Ee2x2'></ul>

                      <tfoot id='Ee2x2'></tfoot>

                      <legend id='Ee2x2'><style id='Ee2x2'><dir id='Ee2x2'><q id='Ee2x2'></q></dir></style></legend>
                    1. <i id='Ee2x2'><tr id='Ee2x2'><dt id='Ee2x2'><q id='Ee2x2'><span id='Ee2x2'><b id='Ee2x2'><form id='Ee2x2'><ins id='Ee2x2'></ins><ul id='Ee2x2'></ul><sub id='Ee2x2'></sub></form><legend id='Ee2x2'></legend><bdo id='Ee2x2'><pre id='Ee2x2'><center id='Ee2x2'></center></pre></bdo></b><th id='Ee2x2'></th></span></q></dt></tr></i><div id='Ee2x2'><tfoot id='Ee2x2'></tfoot><dl id='Ee2x2'><fieldset id='Ee2x2'></fieldset></dl></div>