Qwertenx
Qwertenx

Reputation: 23

Cannot load form yii2

I am newbie to yii2. I am trying to create my simple form in yii2 to retrieve password. Here is class code:

<?php

namespace app\models;
use yii\base\Model;



class RetrievePasswordForm extends Model
{
    public $email;

    public function rules()
    {
        return [
            ['email', 'required'],
            ['email', 'email'],
        ];
    }
}

Here is action code:

$model = new RetrievePasswordForm();
        if ($model->load(Yii::$app->request->post()) && $model->validate()){
            return $this->render('retrievepassword-confirm', ['model' => $model]);
        } else {
            return $this->render('retrievepassword', ['model' => $model]);
        }

My form looks like this:

<?php
use yii\helpers\Html;
use yii\widgets\ActiveForm;
$this->title = 'Retrieve password';
$this->params['breadcrumbs'][] = $this->title;
?>
<h1><?= Html::encode($this->title) ?></h1>
<p>We will send link to retrieve your password to the following email:</p>
<?php $form = ActiveForm::begin(); ?>

<?= $form->field($model, 'email')->textInput(['style'=>'width:200px'])?>

<div class="form-group">
    <?= Html::submitButton('Send', ['class' => 'btn btn-primary']) ?>
</div>

<?php ActiveForm::end(); ?>

The problem is that $model->load(Yii::$app->request->post()) always returns false, so when I am clicking "submit" button, page just reloads. I am currently working without database. I am just creating form and trying to go to another form, when valid data received in model. Thanks for help.

Upvotes: 1

Views: 839

Answers (2)

ScaisEdge
ScaisEdge

Reputation: 133360

Try explicitally assign the method and the action to the active Form

Then Assuming that your target action is named actionRetrivePassword

<?php $form = ActiveForm::begin([
        'method' => 'post',
        'action' => Url::to(['/site/retrivepassword']

  ); ?>

Upvotes: 1

Gynteniuxas
Gynteniuxas

Reputation: 7093

I'll go with I feel it's wrong, if this doesn't help, please give more information.

It's a registration form, so I assume you need email and password for that (since you have those columns in database). But you also declared public member $email in your model. This removes any value associated to $email from database. Therefore, remove this line:

public $email;

Upvotes: 0

Related Questions