【问题标题】:yii2 - dynamic activeform and client validationyii2 - 动态活动表单和客户端验证
【发布时间】:2016-10-15 17:44:22
【问题描述】:

我正在通过 ajax 动态呈现 ActiveForm,但是由于页面加载时表单不存在,所以当表单被回显时,必要的 JS 文件yiiActiveForm.jsyii.validation.js) 和相关的验证触发器不存在出去。

这里是一些示例代码:

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);
}

查看(review-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/,但这里讨论的是向单个属性添加验证,而不是对整个表单。

有人知道怎么做吗?

【问题讨论】:

    标签: yii yii2


    【解决方案1】:

    我找到了解决方案:在回显视图时使用renderAjax() 而不是renderPartial()

    http://www.yiiframework.com/doc-2.0/yii-web-view.html#renderAjax()-detail

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多