2016-10-15 5 views
0

私はActiveフォームをajax経由で動的にレンダリングしていますが、ページロード時にフォームが存在しないため、必要なJSファイルyiiActiveForm.js,yii.validation.js)と関連する検証トリガーは存在しません。フォームがエコーされます。ここでyii2 - 動的アクティブフォームとクライアントの検証

は、いくつかのサンプルコードです:

JS:

$('.btn-edit').on('click', function() { 
    var id = $(this).attr('data-id'); 
    var url = base_url + '/account/editreview/' + id; 

    $.ajax({ 
     type: 'post', 
     url: url, 
     dataType: 'json', 
     success: function(result) { 
      $('.popup').html(result.html); 
     } 
    }); 
}); 

コントローラー:

public function actionEditReview($id) 
{ 
    $return_array = [ 
     'html' => null, 
    ]; 

    $review = $this->findReview($id); 

    $return_array['html'] = $this->renderPartial('review-popup', [ 
     'review' => $review, 
    ]); 

    echo json_encode($return_array); 
} 

ビュー(レビュー-popup.php):

<?php 
use yii\widgets\ActiveForm; 
?> 

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

<?php echo $form->field($review, 'title')->textInput(); ?> 

<button type="submit" class="btn-edit" data-id="<?php echo $review->id; ?>">Submit</button> 

<?php ActiveForm::end(); ?> 

私が持っていますこのページのノートを読むhttps://yii2-cookbook.readthedocs.io/forms-activeform-js/これは、フォーム全体ではなく、単一の属性にバリデーションを追加する方法について説明しています。

誰でもこれを行う方法を知っていますか?

答えて

関連する問題