YII2:' enableAjaxValidation' ' enableClientValidation'不适合客户规则

时间:2015-11-26 07:15:18

标签: php yii yii2

我正在使用YII2 bootstrap / active form。代码如下:
在控制器中:

<?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)它重新加载(之后)重新加载它显示错误,即ajax不会自定义规则功能.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,
  ]);

}

1 个答案:

答案 0 :(得分:3)

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,
    ]);
}

您的控制器代码必须

{{1}}