我是Yii的新手,無法弄清楚ajax驗證如何與ActiveForm
配合使用。 在我的模型我有獨特的領域:使用Ajax,Yii2驗證唯一字段ActiveForm
public function rules()
{
return [
//...
[['end_date'], 'unique'], //ajax is not working
[ 'end_date', //ajax is working
'compare',
'compareAttribute'=>'start_date',
'operator'=>'>=',
'skipOnEmpty'=>true,
'message'=>'{attribute} must be greater or equal to "{compareValue}".'
],
];
}
的比較規則驗證與阿賈克斯和工作正常,但unique
不是。我試圖啓用窗體上的ajax驗證:
<?php $form = ActiveForm::begin(['id' => 'routingForm', 'enableClientValidation' => true, 'enableAjaxValidation' => true]); ?>
但是不知道下一步該怎麼做。
控制器:
public function actionCreate()
{
$model = new Routing();
$cities = [];
if ($model->load(Yii::$app->request->post()) && $model->save()) {
return $this->redirect(['index']);
} else {
return $this->renderAjax('create', [
'model' => $model,
'cities' => $cities
]);
}
}
我知道我可以讓Ajax調用上end_date
變化事件和形式submit
控制器,但不知道如何作出一切適當的CSS來顯示錯誤。
顯示控制器代碼,請... –
@DoubleH,更新 –
@DoubleH,更新 –