2015-11-26 55 views
1

我使用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, 
    ]); 

}

+0

http://www.yiiframework.com/doc-2.0/yii-bootstrap-activeform.html'skipOnEmpty'存在驗證規則而不是表單。 – ineersa

+0

'skipOnEmpty'不是ActiveForm的屬性... –

+0

ok我已經將'skipOnEmpty'=> false,'skipOnError'=> false模型化爲自定義規則函數,例如:「[['category_name'],'checkName', 'message'=>'類別名稱已經存在','skipOnEmpty'=> false,'skipOnError'=> false],「...但是仍然不能工作..」 – Ren

回答

2

skipOnEmpty不是ActiveForm屬性..它是示範規則屬性.. 可規則必須配置爲

public function rules() 
{ 
    return [ 
     [['category_name'], 'string'], 
     [['category_name'], 'required'], 
     [['category_name'], 'string', 'max' => 45], 
     [['category_name'], 'checkName', 'message' => 'Category name is already exists.' ,'skipOnEmpty' => false], 
    ]; 
} 

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.'); 
     } 
} 

您的控制器代碼必須爲

public function actionCreate() 
{ 

    Url::remember(); 
    $model = new Mycategory; 

    if(Yii::$app->request->isAjax){ 
     $model->load(Yii::$app->request->post()); 
     return Json::encode(\yii\widgets\ActiveForm::validate($model)); 
    } 

    $connection = Yii::$app->db; 
    $transaction = $connection->beginTransaction(); 
    if ($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, 
    ]); 
} 
+0

好的我做到了,但請看上面的評論。 – Ren

+0

我已將'skipOnEmpty'=> false,'skipOnError'=> false模型化爲自定義規則函數,如:[['category_name'],'checkName','message'=>'類別名稱已存在' 'skipOnEmpty'=> false,'skipOnError'=> false],「...但仍然不工作。」 – Ren

+0

如果我只做ajaxvalidation true不是客戶端,那麼ajax可以工作,但不會打印任何錯誤消息。沒有AJAX它打印錯誤後重新加載,但我想這裏的AJAX – Ren