我有一個自定義驗證方法的模型。爲了測試它總是返回一個錯誤信息。Yii2自定義客戶端驗證與模式的AJAX渲染
public function rules()
{
return [
...
['staff_ids', 'each', 'rule' => ['string']],
[['staff_ids'], 'validateStaffIds'],
...
];
}
public function validateStaffIds($attribute, $params, $validator) {
$this->addError($attribute, 'There is an error in the staff ids');
}
在view.php是模態元素
<p>
<?= Html::button('Add Ensemble Staff',
['value' => Url::to(['ensemble/add', 'id' => $model->id]),
'title' => 'Adding New Ensemble Staff',
'class' => 'showModalButton btn btn-primary']);
?>
</p>
<?php
Modal::begin([
'closeButton' => [
'label' => 'x',
],
'headerOptions' => ['id' => 'modalHeader'],
'id' => 'modal',
'size' => 'modal-lg',
]);
echo "<div id='modalContent'></div>";
Modal::end();
?>
這一切都激發了JS代碼...
$(function(){
$(document).on('click', '.showModalButton', function(){
if ($('#modal').data('bs.modal').isShown) {
$('#modal').find('#modalContent')
.load($(this).attr('value'));
} else {
//if modal isn't open; open it and load content
$('#modal').modal('show')
.find('#modalContent')
.load($(this).attr('value'));
}
//dynamiclly set the header for the modal
...
});
});
而且ensemble
控制器,處理add
行動
public function actionAdd($id)
{
$model = $this->findModel($id);
// in the post ('ensembleStaff_ids' => [0 => '2']); where the id actually is staff_id
if ($model->load(Yii::$app->request->post()) && $model->save()) {
return $this->redirect(['view', 'id' => $id]);
} else {
return $this->renderAjax('add', [
'model' => $model,
]);
}
}
這是由JS注入模型(
Url::to(['ensemble/add', 'id' => $model->id]),
)
<?php $form = ActiveForm::begin(['id' => 'add-theater-stuff-form']); ?>
<?= $form->field($model, 'staff_ids')->widget(Select2::className(), [
'model' => $model,
'data' => ArrayHelper::map(app\models\TheaterStaff::find()->where(['theater_id' => $model->theater_id])->all(), 'staff_id', 'staff.fullname'),
'options' => [
'multiple' => true,
'prompt' => 'Ensemble Staff',
],
'pluginOptions' => [
'tags' => true
]
]); ?>
<div class="form-group">
<?= Html::submitButton('Add', ['class' => 'btn btn-primary']) ?>
</div>
<?php ActiveForm::end(); ?>
點擊Add Ensemble Staff
按鈕
和表單正常工作,並提出了模態窗口。目前爲止表格本身運行良好;也是默認的驗證工作。即使自定義驗證被調用,但返回$ this-> renderAjax(...)不再加載在模態窗口中;它是分開的。
A picture顯示模式加載,提交後的結果和默認驗證的模態。
我發現了類似的問題here。但是在表單中添加一個id並不能解決問題。那麼如何讓默認驗證在模態窗口中正確顯示?有人有線索嗎?
解決方案
感謝您的答覆。對於我的解決辦法是: 形式啓用AJAX
<?php $form = ActiveForm::begin(['id' => 'add-ensemble-stuff-form', 'enableAjaxValidation' => true]); ?>
而且在控制器添加以下邏輯
public function actionAdd($id)
{
$model = $this->findModel($id);
if (Yii::$app->request->isAjax && $model->load(Yii::$app->request->post())) {
Yii::$app->response->format = Response::FORMAT_JSON;
return ActiveForm::validate($model);
} else {
// in the post ('ensembleStaff_ids' => [0 => '2']); where the id actually is staff_id
if ($model->load(Yii::$app->request->post()) && $model->save()) {
return $this->redirect(['view', 'id' => $id]);
} else {
return $this->renderAjax('add', [
'model' => $model,
]);
}
}
}
嘗試'enableAjaxValidation = >表單選項中的true>。 –
@InsaneSkull是的工作!我真的認爲我已經嘗試過了...... hrrr ..謝謝! – Luc