【发布时间】:2016-10-15 17:44:22
【问题描述】:
我正在通过 ajax 动态呈现 ActiveForm,但是由于页面加载时表单不存在,所以当表单被回显时,必要的 JS 文件yiiActiveForm.js、yii.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/,但这里讨论的是向单个属性添加验证,而不是对整个表单。
有人知道怎么做吗?
【问题讨论】: