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 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥15 Python3.5 相关代码写作
  • ¥20 测距传感器数据手册i2c
  • ¥15 RPA正常跑,cmd输入cookies跑不出来
  • ¥15 求帮我调试一下freefem代码
  • ¥15 matlab代码解决,怎么运行
  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗