Reputation: 3414
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]);
}
}
<?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(); ?>
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',
];
}
}
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.
Upvotes: 0
Views: 6369
Reputation: 133380
The problem is related to the fact you are using a
<?= $form->field($model, 'repeat_password') ?>
that not exist for the User Model ..
you should create a a FormModel (eg: UserInput with also this repeat_password
field ) for manage the correct input and then in action mange properly the assigment from your formModel/UserInput model and User model for store
for build a proper form model class you can take a looka at this guide http://www.yiiframework.com/doc-2.0/guide-start-forms.html
Upvotes: 1
Reputation: 3450
You have to add new public property to User.php
(model class file) as
class User extends ActiveRecord
{
public $repeat_password;
Please refer to this first
http://www.yiiframework.com/doc-2.0/guide-structure-models.html http://www.yiiframework.com/doc-2.0/guide-start-forms.html
It will not take much time.
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
Upvotes: 1