0

I am new to Yii, and cannot figure out how ajax validation works with ActiveForm. In my model I have unique field:

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}".'
            ],
        ];
    }

The compare rule is validated with ajax and works fine, but unique is not. I tried to enable ajax validation on form:

  <?php $form = ActiveForm::begin( ['id' => 'routingForm', 'enableClientValidation' => true, 'enableAjaxValidation' => true]); ?>

But don't know what to do next.

Controller:

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

I know I could make ajax call to controller on end_date change event and form submit, but not sure how make all appropriate css to show the errors.

2
  • show your controller code please... Commented Dec 24, 2015 at 4:36
  • 1
    Just read the official docs, it's covered here. Commented Dec 24, 2015 at 4:47

2 Answers 2

1

You need to use Yii::$app->request->isAjax in controller.

public function actionCreate()
{
    if (Yii::$app->request->isAjax && $model->load(Yii::$app->request->post())) {
        Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;
        return ActiveForm::validate($model);
      }
   if ($model->load(Yii::$app->request->post()) && $model->save()) {
       return $this->redirect(['index']);
    } else {
        return $this->renderAjax('create', [
          'model' => $model,
          'cities' => $cities
        ]);
}
}
Sign up to request clarification or add additional context in comments.

Comments

1

Try this code in your controller...

public function actionCreate()
{
    $model = new Routing();
    $cities = []; 

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

    if ($model->load(Yii::$app->request->post()) && $model->save())  {            
        return $this->redirect(['index']);
   } else {
    return $this->renderAjax('create', [
        'model' => $model,
        'cities' => $cities
    ]);
}
}

1 Comment

You should not use Json::encode , either change response format on the fly or use ContentNegotiator.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.