/** * Action for AJAX form validation * * @return array */ public function actionIndex() { $model = new Comment(['scenario' => Yii::$app->user->isGuest ? Comment::SCENARIO_GUEST : Comment::SCENARIO_USER]); if (Yii::$app->request->isAjax && $model->load(Yii::$app->request->post())) { Yii::$app->response->format = Response::FORMAT_JSON; return ActiveForm::validate($model); } }
public function run() { $commentsAsset = CommentsAsset::register($this->getView()); CommentModule::getInstance()->commentsAssetUrl = $commentsAsset->baseUrl; $model = $this->model; $model_id = $this->model_id; $comment = new Comment(compact('model', 'model_id')); $comment->scenario = Yii::$app->user->isGuest ? Comment::SCENARIO_GUEST : Comment::SCENARIO_USER; if ((!CommentModule::getInstance()->onlyRegistered || !Yii::$app->user->isGuest) && $comment->load(Yii::$app->getRequest()->post())) { if ($comment->validate() && Yii::$app->getRequest()->validateCsrfToken() && Yii::$app->getRequest()->getCsrfToken(true) && $comment->save()) { if (Yii::$app->user->isGuest) { CommentsHelper::setCookies(['username' => $comment->username, 'email' => $comment->email]); } return Yii::$app->getResponse()->redirect(Yii::$app->request->referrer); } } return $this->render('comments', compact('model', 'model_id', 'comment')); }
public function run() { $commentsAsset = CommentsAsset::register($this->getView()); CommentModule::getInstance()->commentsAssetUrl = $commentsAsset->baseUrl; $model = $this->model; $model_id = $this->model_id; $comment = new Comment(compact('model', 'model_id')); $comment->scenario = Yii::$app->user->isGuest ? Comment::SCENARIO_GUEST : Comment::SCENARIO_USER; if ((!CommentModule::getInstance()->onlyRegistered || !Yii::$app->user->isGuest) && $comment->load(Yii::$app->getRequest()->post())) { if ($comment->validate() && Yii::$app->getRequest()->validateCsrfToken() && Yii::$app->getRequest()->getCsrfToken(true) && $comment->checkSpam() && $comment->save()) { if (Yii::$app->user->isGuest) { CommentsHelper::setCookies(['username' => $comment->username, 'email' => $comment->email]); } Yii::$app->getResponse()->redirect(Yii::$app->request->referrer); return; } } $dataProvider = new ActiveDataProvider(['query' => Comment::find(true)->where(['model' => $model, 'model_id' => $model_id, 'parent_id' => NULL, 'status' => Comment::STATUS_PUBLISHED]), 'pagination' => ['pageSize' => CommentsModule::getInstance()->commentsPerPage, 'pageParam' => 'comment-page', 'pageSizeParam' => 'comments-per-page'], 'sort' => ['defaultOrder' => ['created_at' => CommentsModule::getInstance()->orderDirection]]]); return $this->render('comments', compact('model', 'model_id', 'comment', 'dataProvider')); }