异常(未知属性)"yii \ base \ UnknownPropertyException",并显示消息“获取未知属性:app \ models \ User :: repeat_password" [英] Exception (Unknown Property) 'yii\base\UnknownPropertyException' with message 'Getting unknown property: app\models\User::repeat_password'

查看:165
本文介绍了异常(未知属性)"yii \ base \ UnknownPropertyException",并显示消息“获取未知属性:app \ models \ User :: repeat_password"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Yii框架的新手.这是我在stackoverflow中针对相关框架的第一篇文章.我的问题是如何在视图页面中显示非模型输入字段.请检查我的控制器并查看代码.

I am new in Yii framework. This is my first post in stackoverflow for related framework. My problem is how to show non model input field in the view page. Please check my controler and view code.

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]);
    }
}

views(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 不在我的表列中.这是一个非模型输入字段.所以我得到以上错误信息.请帮助我,让我知道如何解决它.

I have 3 column in my user table. Columns are username, email and password but repeat_password is not in my table column. This is a non model input field. So I am getting above error message. Please help me and let me know how to fix it.

推荐答案

您必须以

class User extends ActiveRecord
{
    public $repeat_password;

请先参考此

http://www.yiiframework.com/doc-2.0/guide-structure-models.html http://www.yiiframework.com/doc-2.0/guide-start-forms.html

这不会花费很多时间.

别忘了将其验证规则添加到 User.php rules 方法中 http://www.yiiframework.com/doc-2.0/guide-structure-models.html#safe-属性

Don't forget to add its validation rules in rules method of User.php http://www.yiiframework.com/doc-2.0/guide-structure-models.html#safe-attributes

这篇关于异常(未知属性)"yii \ base \ UnknownPropertyException",并显示消息“获取未知属性:app \ models \ User :: repeat_password"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆