我使用YII2引導/主動form.The代碼如下:
在控制器:
YII2: 'enableAjaxValidation', 'enableClientValidation' 不工作的custome規則
<?php $form = ActiveForm::begin(['id' => 'form-terms','enableAjaxValidation' => false,'enableClientValidation' => true,'skipOnEmpty' => false, 'skipOnError' => false]); ?>
....
在型號:
public function rules()
{
return [
[['category_name'], 'string'],
[['category_name'], 'required'],
[['category_name'], 'string', 'max' => 45],
[['category_name'], 'checkName', 'message' => 'Category name is already exists.'],
];
}
public function checkName($attribute) {
$model = Mycategory::find()->where('category_name = "' . $this->$attribute . '" AND status != "1"'->all();
if (count($model) > 0) {
$this->addError($attribute, 'Category name is already exists.');
}
}
的問題是,我現在面臨的問題是,沒有Ajax的形式提交正常工作與正確的消息,但是當我使用AJAX如上它給了我錯誤:Setting unknown property: yii\bootstrap\ActiveForm::skipOnEmpty and if I remove these skip arguments ajax works fine for required field but in custom rule(checkName function)
它重新加載(重新加載後顯示錯誤,即阿賈克斯不爲自定義規則函數trigerring。 Ajax只適用於所需的規則罰款)。什麼是問題?
編輯:控制器代碼:
public function actionCreate() {
Url::remember();
$model = new Mycategory;
$connection = Yii::$app->db;
$transaction = $connection->beginTransaction();
if ($model->load(Yii::$app->request->post())) {
$model->load(Yii::$app->request->post());
$valid = $model->validate();
if ($valid) {
try {
$model->save();
$transaction->commit();
Yii::$app->getSession()->setFlash('success', Yii::t('app', 'Category is successfully added.'));
return $this->redirect('index');
} catch (Exception $e) {
$transaction->rollBack();
Yii::$app->getSession()->setFlash('error', Yii::t('app', $e->getMessage()));
return $this->render('create', [
'model' => $model,
]);
}
} else {
Yii::$app->getSession()->setFlash('error', Yii::t('app', 'Please change a few things up and try submitting again. '));
}
}
return $this->render('create', [
'model' => $model,
]);
}
http://www.yiiframework.com/doc-2.0/yii-bootstrap-activeform.html'skipOnEmpty'存在驗證規則而不是表單。 – ineersa
'skipOnEmpty'不是ActiveForm的屬性... –
ok我已經將'skipOnEmpty'=> false,'skipOnError'=> false模型化爲自定義規則函數,例如:「[['category_name'],'checkName', 'message'=>'類別名稱已經存在','skipOnEmpty'=> false,'skipOnError'=> false],「...但是仍然不能工作..」 – Ren