我知道这是一个愚蠢的问题,但我刚开始学习Yii2。我没有在这里找到任何与此有关的有用信息,所以。我需要做的是在用户成功添加到数据库时显示一条消息。可能somoene帮我解决这个问题吗?我不知道它必须写在哪里:在model
,controller
或view
。如果在Yii2中将用户添加到数据库中,如何显示成功消息?
这里是我的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,
]);
}
这里是我的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>
现在正在发生的事情是该脚本不允许进入的日期,这比今天的后日期,也不允许在日期字段中输入字符串。所以要清楚的是,如果用户输入了正确的信息,我需要添加会显示“用户已成功输入数据库”的消息。
感谢您的帮助!
我只是试图粘贴您编辑的代码,但它并没有奏效。它不会打印任何消息,当用户正确输入字段时:(这就像我的旧代码,只是添加它,就是这样:) – HELPME
你的布局中有'Alert :: widget()'吗? – gmc
'layout'是模型中的函数'behaviour()'还是对不起愚蠢的问题,但只是试图学习 – HELPME