I have a an ActiveForm in my view.
In my controller I validate the model, and return the errors if any.
What I would like is to be able to show a Modal (a custom JavaScript modal I do call) whenever ActiveForm ajax does return the errors.
This is my current controller:
<?php
namespace app\controllers;
use app\models\CCoupon;
use Yii;
use app\components\Utils;
use app\components\BaseController;
use yii\helpers\Url;
use yii\web\Response;
use yii\widgets\ActiveForm;
class CampaignsController extends BaseController
{
public function actions()
{
return [
'error' => [
'class' => 'yii\web\ErrorAction',
],
];
}
public function actionValidate()
{
$data = [];
$model = new CCoupon();
$model->setScenario('validate');
$model->load(Yii::$app->request->post());
if (Yii::$app->request->isAjax) {
Yii::$app->response->format = Response::FORMAT_JSON;
$errors = ActiveForm::validate( $model );
if (!empty($errors)) {
return $errors;
} else {
$coupon = CCoupon::findOne(['code' => $model->code]);
if (empty($coupon->used_at)) {
$current_time = new \DateTime();
$current_time = $current_time->format('Y-m-d H:i:s');
$coupon->used_at = $current_time;
}
}
if ($coupon->save()) {
Yii::$app->session->setFlash('success', 'Well done!.');
} else {
Yii::$app->session->setFlash('error', 'There was an error!');
}
return $this->refresh();
}
$data['model'] = $model;
return $this->render('validate', $data);
}
}
Currently when I’m being able to save, I’m setting a flash message and refreshing the page so the flash message shows up. However I would prefer to show a success modal (just like I want to did with the errors).
Any ideas?
Thanks