2012-08-17 50 views
3

我在靜態表單中使用Yii用戶端驗證,這很好。 但我不知道如何爲ajax加載的元素添加驗證器。Yii - 用戶端驗證ajax加載表單元素

我有簡單的窗體小部件,我想通過AJAX加載更多的輸入字段(這對小jQuery腳本沒有問題)。但我不知道如何添加的Yii驗證器的JavaScript加載的元素 - 我的意思是創建JS汽車驗證器,如:

<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> 

有什麼辦法如何添加或刪除驗證?

+0

以標準方式添加驗證器:通過渲染使用適當輸入元素渲染器的視圖(例如,與[本](http://www.yiiframework.com/doc/api/1.1/CHtml#activeTextField-detail)和類似的方法或通過'CActiveForm') – Jon 2012-08-17 09:39:49

+0

我已經有了它們。但我有另一個問題: 我有驗證器的元素: '$ email = new Yp_Form_Element(Yp_Form_Element :: FIELD_textField,'email',$ this); $ email-> setRequired()' 但Yii在渲染頁面後創建了javascript客戶端驗證腳本。但我需要爲這個腳本添加一些Ajax加載元素。 – emte 2012-08-17 10:22:52

+0

如何渲染所有輸入並使用jquery隱藏它們,而不是通過ajax加載它們。如果沒有太多的話。 – Asgaroth 2012-08-27 15:19:02

回答

0

這是有點棘手...如果你想這樣做使用yiiactiveform它,你必須:

  1. 一個字段添加到驗證或
  2. 從確認刪除字段

我的建議是:

  1. 創建自己的JavaScript驗證功能(可能要牛逼在這裏當您在野外裝入形式O副本警予的驗證碼)

    $('#newsletter-form-footer').submit(function() { 
         return MyValidator.validate(); 
    }); 
    
  2. //You have to get rulesORoptions 
    // along with fieldName from your ajax request. 
    MyValidator.add(fieldName, rulesORoptions); 
    
  3. 當你刪除一個字段的表格

    MyValidator.remove(fieldName); 
    
  4. MyValidator代碼這裏...

    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; 
         }, 
         ..... 
         ..... 
    };