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条)

报告相同问题?

悬赏问题

  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料