L
L
liltrill2020-01-03 07:29:27
Yii
liltrill, 2020-01-03 07:29:27

How in Yii2 after adding data to clear the form and display a flash message on the same page without resubmitting?

public function actionAdd()
{
    $model = new News();

    if ($model->load(Yii::$app->request->post())) {
        if ($model->save()) {
            Yii::$app->session->setFlash('success', 'Удача: Новость добавлена!');
            $model = new News();
        } else {
            Yii::$app->session->setFlash('error', 'Ошибка: Не удалось добавить новость!');
        }
    }

    return $this->render('add', compact('model'));
}

And here is such a horror when refreshing the page:
5e0ec23330848537983155.png

Answer the question

In order to leave comments, you need to log in

1 answer(s)
E
Eugene, 2020-01-03
@liltrill

You need to do a redirect after the form entry in order to drop the post. And in session there will be a flash message.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question