dousui6488 2017-04-06 08:59 采纳率: 100%
浏览 24
已采纳

如果在Yii2中将用户添加到数据库中,如何显示成功消息?

I know this is a dumb question, but I just started learning with Yii2. I haven't found any useful information here related to this, so. What I need to do is display a message if the user was added to the database successfuly. Could somoene help me to solve this? I've no idea where it has to be written: in a model, controller or view.

Here is my controller action:

    public function actionCreate()
{
    $model = new Employee();
    $model->scenario = Employee::SCENARIO_CREATE;

    $post = Yii::$app->request->post();

    if ($model->load($post) && $model->save()) {
        return $this->redirect(['create']);
    }

    return $this->render('create', [
        'model' => $model,
    ]);
}

Here is my view:

<div class="employee-form">

<?php $form = ActiveForm::begin(); ?>

<?= $form->field($model, 'name')->textInput([
    'maxlength' => 50,

]) ?>
<?= $form->field($model, 'surname')->textInput([
    'maxlength' => 50,
]) ?>
<?= $form->field($model, 'employment_date')->textInput() ?>

<div class="form-group">
    <?= Html::submitButton($model->isNewRecord ? Yii::t('app', 'Create') : Yii::t('app', 'Update'), ['class' => $model->isNewRecord ? 'btn btn-success' : 'btn btn-primary']) ?>
</div>

Now what is happening is that the script doesn't allow to enter the date, which is later than today's date and also doesn't allow to enter a string into the date field. So to be clear, if the user has entered correct information, I need to add the message that would say "Users has been entered to the database successfully".

Thanks for any help!

  • 写回答

2条回答 默认 最新

  • dongsuo0517 2017-04-06 09:09
    关注

    Set flash message in your controller. like below.

     Yii::$app->session->setFlash('flashMsg', 'flash Msg or any kind of content like variables');
    

    and show this message in your view page. like below.

    <?php if (Yii::$app->session->hasFlash('flashMsg')){ ?>
        <div class="alert alert-success">
            <!-- flash message -->
             <?php Yii::$app->session->getFlash('flashMsg'); ?>
        </div>
    <?php } ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化