【发布时间】:2023-04-10 13:59:01
【问题描述】:
我是 Yii 框架的新手。这是我在 stackoverflow 中针对相关框架的第一篇文章。我的问题是如何在视图页面中显示非模型输入字段。请检查我的控制器并查看代码。
控制器
public function actionRegister() {
$model = new User();
if ($model->load(Yii::$app->request->post()) && $model->validate()) {
//Insert data code here...
return $this->render('entry-confirm', ['model' => $model]);
} else {
return $this->render('entry', ['model' => $model]);
}
}
视图(entry.php)
<?php
use yii\helpers\Html;
use yii\widgets\ActiveForm;
?>
<?php $form = ActiveForm::begin(); ?>
<?= $form->field($model, 'username') ?>
<?= $form->field($model, 'email') ?>
<?= $form->field($model, 'password')->passwordInput() ?>
<?= $form->field($model, 'repeat_password')->passwordInput() ?>
<div class="form-group">
<?= Html::submitButton('Submit', ['class' => 'btn btn-primary']) ?>
</div>
<?php ActiveForm::end(); ?>
模型(User.php)
namespace app\models;
use Yii;
class User extends \yii\db\ActiveRecord {
public static function tableName() {
return 'user';
}
public function rules() {
return [
[['username', 'password', 'email'], 'required'],
[['username', 'password', 'email'], 'string', 'max' => 128],
['email', 'email', 'message' => "The email isn't correct"],
['email', 'unique', 'message' => 'Email already exists!'],
['repeat_password', 'required'],
[['repeat_password'], 'string', 'min'=>6, 'max'=>40],
['password', 'compare', 'compareAttribute'=>'repeat_password'],
];
}
public function attributeLabels() {
return [
'id' => 'ID',
'username' => 'Username',
'password' => 'Password',
'email' => 'Email',
];
}
}
输出
我的user 表中有 3 列。列是username、email 和password,但repeat_password 不在我的表格列中。这是一个非模型输入字段。所以我收到了上面的错误消息。请帮助我,让我知道如何解决它。
【问题讨论】:
-
更新您的问题添加您的相关模型
-
@scaisEdge 我已经更新了我的问题。