2016-07-29 92 views
0

在我的代碼有一個字段(例如字段名稱爲exampleField []),其中我實現通過JS。現在添加更多功能,我想要實現Yii2 需要Validtor在每個領域。我已經使用每個驗證器,但它不工作。我的yii2版本是2.0.6。這裏在先進的感謝Yii2需要驗證在陣列字段

是我的javascript代碼

var max_fields = 4; //maximum input boxes allowed 
     var wrapper = $(".addAmount"); //Fields wrapper 
     var add_button = $(".add_field_button"); //Add button ID 
     var html = ''; 

     var x = <?php echo $count ?>; //initlal text box count 

     $(add_button).click(function (e) { //on add input button click 

      html = '<div id="multipleRow' + x + '"><div class="fullwidth">'; 
      html += '<div class="span4"><div class="control-group">'; 
      html += '<label for="plandetail-planduration" class="control-label">Duration <span class="required">*</span></label>'; 
      html += '<div class="controls"><div class="form-group field-plandetail-planduration required">'; 
      html += '<input type="text" name="PlanDetail[planDuration][]" class="form-control" id="plandetail-planduration">'; 
      html += '<div class="error"></div></div></div></div>'; 
      html += '</div>'; 
      html += '<div class="span4"><div class="control-group">'; 
      html += '<label for="plandetail-plandurationtype" class="control-label">Duration Period<span class="required">*</span></label>'; 
      html += '<div class="controls"><div class="form-group field-plandetail-plandurationtype">'; 
      html += '<select name="PlanDetail[planDurationType][]" class="form-control" id="plandetail-plandurationtype">'; 
      html += '<option value=""></option>'; 
      html += '<option value="month">Month</option>'; 
      html += '<option value="year">Year</option>'; 
      html += '</select>'; 
      html += '<div class="help-block"></div>'; 
      html += '</div></div></div></div></div>'; 
      html += '<div class="fullwidth">'; 
      html += '<div class="span4"><div class="control-group">'; 
      html += '<label for="plandetail-planamount" class="control-label">Amount <span class="required">*</span></label>'; 
      html += '<div class="controls"><div class="form-group field-plandetail-planamount required">'; 
      html += '<input type="text" name="PlanDetail[planAmount][]" class="form-control" id="plandetail-planamount">'; 
      html += '<div class="error"></div>'; 
      html += '</div></div>'; 
      html += '</div></div>'; 
      html += '<div class="span4"><div class="control-group">'; 
      html += '<label for="plandetail-plandiscountamount" class="control-label">Discount Amount <span class="required">*</span></label>'; 
      html += '<div class="controls"><div class="form-group field-plandetail-plandiscountamount required">'; 
      html += '<input type="text" name="PlanDetail[planDiscountAmount][]" class="form-control" id="plandetail-plandiscountamount">'; 
      html += '<div class="error"></div>'; 
      html += '</div></div>'; 
      html += '</div></div>'; 
      html += '<div class="span4"><div class="control-group">'; 
      html += '<label for="plandetail-plancurrency" class="control-label">Currency <span class="required">*</span></label>'; 
      html += '<div class="controls"><div class="form-group field-plandetail-plancurrency required">'; 
      html += '<select name="PlanDetail[planCurrency][]" class="form-control" id="plandetail-plancurrency">'; 
      html += '<option value=""></option><option value="USD">USD</option><option value="EUR">EUR</option>'; 
      html += '</select>'; 
      html += '<div class="help-block"></div>'; 
      html += '</div></div>'; 
      html += '</div></div>'; 
      html += '</div>'; 
      html += '<div class="col-lg-4"><a href="javascript:void(0)" class="remove_field" id="' + x + '">Remove</a></div>'; 
      html += '</div></div>'; 
      e.preventDefault(); 
      if (x < max_fields) { //max input box allowed 
       x++; //text box increment 
       $(wrapper).append(html); //add input box 
      }else{alert('You Can not add more than 4 Amount')} 

     }); 
     $(wrapper).on("click", ".remove_field", function (e) { //user click on remove text 
      e.preventDefault(); 
//   alert(this.id); 
      $("#multipleRow" + this.id).remove(); 
      x--; 
     }); 
+0

EachValidator應該工作。我想這可能是你的JavaScript產生這些領域的錯誤。使用相關代碼更新您的問題,以便人們可以提供更多幫助。到目前爲止,我們只能猜測發生了什麼。 – Clyff

+0

請把你的代碼 – yafater

+0

@Clyff我更新了我的代碼 – SAR

回答

0

你只添加你輸入的html和失蹤的js,使驗證。您必須將所有動態字段添加到yiiActiveForm。這裏是自帶的基本模板的接觸形式的例子:

首先,從表單中刪除領域的電子郵件,因此我們可以將其添加後者js

<?php // $form->field($model, 'email') ?> 

確保您正在使用AJAX驗證表單中的:現在

<?php $form = ActiveForm::begin(['id' => 'contact-form', 'enableAjaxValidation' => true]); ?> 

,在視圖的末尾添加以下代碼:

<?php 
$js = <<<JS 
$(document).ready(function() { 
    var input = '<div class="form-group field-contactform-email required">' + 
     '<label class="control-label" for="contactform-email">Email</label>' + 
     '<input type="text" id="contactform-email" class="form-control" name="ContactForm[email]">' + 
     '<p class="help-block help-block-error"></p>' + 
    '</div>'; 

    $('#contact-form').append(input); // add the html in the form 

    $('#contact-form').yiiActiveForm('add', { // add the js rules to the form 
     'id': 'contactform-email', 
     'name': 'email', 
     'container': '.field-contactform-email', 
     'input': '#contactform-email', 
     'enableAjaxValidation':true 
    }); 
}); 
JS; 

$this->registerJs($js); // insert javascript 

最後,我們只需要做出ajax validation控制器,是這樣的:

use yii\web\Response; 
use yii\widgets\ActiveForm; 

class SiteController extends Controller 
{ 
    ... 

    public function actionContact() 
    { 
     $model = new ContactForm(); 
     if ($model->load(Yii::$app->request->post())) { // load the post 
      if (Yii::$app->request->isAjax) { // verify is ajax 
       Yii::$app->response->format = Response::FORMAT_JSON; // change the response to json format 
       return ActiveForm::validate($model); // return the validation as json 
      } elseif ($model->contact(Yii::$app->params['adminEmail'])) { 
       Yii::$app->session->setFlash('contactFormSubmitted'); 

       return $this->refresh(); 
      } 
     } 

     return $this->render('contact', [ 
      'model' => $model, 
     ]); 
    } 
}