我是yii2的新手。我试图在yii2中创建我的简单表单来检索密码。下面是类代码:无法加载表单yii2
<?php
namespace app\models;
use yii\base\Model;
class RetrievePasswordForm extends Model
{
public $email;
public function rules()
{
return [
['email', 'required'],
['email', 'email'],
];
}
}
这里是动作代码:
$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]);
}
我的形式如下:
<?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(); ?>
的问题是,$model->load(Yii::$app->request->post())
总是返回false,所以当我点击“提交”按钮,页面才会重新加载。 我目前在没有数据库的情况下工作。我只是在创建表单并尝试转到其他表单时,在模型中收到有效数据时。感谢帮助。
两件事情:怎么是你的数据库表看起来像,你的表单看起来如何? –
尝试不使用&& $ model-> validate()..可能在验证中存在一些问题.. – scaisEdge
@scaisEdge我试过了。没有变化。问题是在负载方法中定义。 – Qwertenx