gpt4 book ai didi

javascript - 当使用 ajax 调用渲染表单时,客户端验证在 Yii2 中不起作用

转载 作者:行者123 更新时间:2023-11-28 13:25:30 25 4
gpt4 key购买 nike

使用ajax渲染表单:

<?php $form = ActiveForm::begin(['id' => 'que',
'enableClientValidation' => true
]); ?>
<?php echo $form->field($model, 'fk_subject')
->dropDownList(ArrayHelper::map($subjects, 'id_subject', 'name'), [
'class' => 'form-control ng-pristine ng-valid ng-touched',
'ng-model' => 'que.fk_subject',
'prompt' => 'Select subject',
'ng-change' => 'fillTopic(que);'
]); ?>
<?php echo $form->field($model, 'fk_topic')
->dropDownList([], [
'class' => 'form-control ng-pristine ng-valid ng-touched',
'ng-model' => 'que.fk_topic',
'prompt' => 'Select topic',
'ng-change' => 'openAddTopic(que);',
'ng-options' => 'topic.id_subject as topic.name for topic in topics',
]); ?>
<?php echo $form->field($model, 'type')
->dropDownList($questionTypes, [
'class' => 'form-control ng-pristine ng-valid ng-touched',
'prompt' => 'Select question type',
'ng-model' => 'que.type',
'ng-change' => 'addAnswerOptions(que);',
]); ?>
<?php echo Html::submitButton('Save',
['class' => 'btn btn-primary',
'name' => 'Save']) ?>
<?php ActiveForm::end(); ?>

我在 yii2 中使用 ajax 加载了上述表单,问题是客户端验证无法使用它。如果我在窗口加载时加载表单内容,它就可以正常工作。

最佳答案

好的,我找到了解决方案,我将不得不使用 renderAjax 来获取表单 html,或者必须添加 beginpagebeginend 函数如果我使用 renderPartial

<?php $this->beginPage(); ?>
<?php $this->beginBody(); ?>
// Form and content redenered using ajax
<?php $this->endBody(); ?>
<?php $this->endPage(); ?>

以便 yii 可以添加带有渲染内容的客户端验证脚本

关于javascript - 当使用 ajax 调用渲染表单时,客户端验证在 Yii2 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29592316/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com