2017-01-22 58 views
1

我是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,所以当我点击“提交”按钮,页面才会重新加载。 我目前在没有数据库的情况下工作。我只是在创建表单并尝试转到其他表单时,在模型中收到有效数据时。感谢帮助。

+1

两件事情:怎么是你的数据库表看起来像,你的表单看起来如何? –

+0

尝试不使用&& $ model-> validate()..可能在验证中存在一些问题.. – scaisEdge

+0

@scaisEdge我试过了。没有变化。问题是在负载方法中定义。 – Qwertenx

回答

1

我会去我觉得这是错的,如果这没有帮助,请提供更多信息。

这是一个注册表单,所以我假设你需要电子邮件和密码(因为你在数据库中有这些列)。但你也在你的模型中宣布了公共成员$email。这会从数据库中删除与$email相关的任何值。因此,删除此行:

public $email; 
+0

我目前正在没有db的工作。我只是创建表单并尝试转到另一个表单,在模型中接收到有效数据时。 – Qwertenx

+0

你可以检查这样的错误:'$ model-> load(Yii :: $ app-> request-> post()); $ model-> validate()'然后:'var_dump($ model-> errors);退出;'。提交表单后,您应该能够看到没有任何样式的纯文本。 –

+0

它只显示数组(0){}。 – Qwertenx

0

尝试explicitally分配的方法和措施,以活性形式

然后假设你的目标的行动被命名为actionRetrivePassword

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

); ?> 
+0

对不起,请你解释一下如何写这个网址?如果我的控制器的名称是SiteController并且操作的名称是actionRetrievepassword,那么应该在url中写入什么? – Qwertenx

+0

@ Qwertenx答案更新.. – scaisEdge

+0

谢谢,但不幸的是,没有变化。 – Qwertenx