noobkoder
noobkoder

Reputation: 97

Displaying a pop up message and validation Yii2

In my view.php I have a close ticket button. When I select that button it will automatically get the system time and the status will be change into 'Done'.

What I want to try is that when I click the close button if the ticket has already been done, the time_end won't be changed and it will display a popup message similar to the delete button.

Here's my code:

public function actionClose($id)
{
    $model = $this->findModel($id);
 //     $model->status = ('Done');
 //     $model->time_end = date('y-m-d h-i-s');
 //     $model->save();
 //     return $this->redirect(['view', 'id' => $model->id]);

    if ($model->status == 'Done' && $model->time_end == date('y-m-d h-i-s')) {
        (['data' => ['prompt' => 'Ticket has already been closed!']]);
    } else {
        $model->status = ('Done');
        $model->time_end = date('y-m-d h-i-s');
        $model->save();
        return $this->redirect(['view', 'id' => $model->id]);

}

My problem is that when click the close button and I select a ticket that has a status 'Done' and already has a time end, it still gets the time_end even though the ticket status is already 'Done'. How do I prevent it from happening like some sort of validation with a pop up message similar to the delete button in the view.php

Upvotes: 3

Views: 2200

Answers (2)

nicolascolman
nicolascolman

Reputation: 579

You can use a flash message.

Controller:

if ($model->status == 'Done') {
   Yii::$app->session->setFlash('error', 'Ticket has already been closed!');
    return $this->redirect(['view', 'id' => $model->id]);
}

View:

 <?= Yii::$app->session->getFlash('error'); ?>

Upvotes: 3

noobkoder
noobkoder

Reputation: 97

I finally get it my if else condition is wrong so what I did was this:

public function actionClose($id)
{
  $model = $this->findModel($id);
  //     $model->status = ('Done');
  //     $model->time_end = date('y-m-d h-i-s');
  //     $model->save();
  //     return $this->redirect(['view', 'id' => $model->id]);

if ($model->status == 'Done') {
    (['data' => ['prompt' => 'Ticket has already been closed!']]);
} else {
    $model->status = ('Done');
    $model->time_end = date('y-m-d h-i-s');
    $model->save();
    return $this->redirect(['view', 'id' => $model->id]);

}

But still I don't have any idea on how am I going to display a pop up message.

Upvotes: 0

Related Questions