在SiteController.php中設置方法
public function actionThree()
{
$model = new Three();
if($model->load(Yii::$app->request->post()) && $model->validate()) {
Yii::$app->session->setFlash('success','Do it easy,Lesson 3');
}
return $this->render('three',['model'=>$model]);
}
新建一個模型類 Three.php
<?php
namespace appmodels;
use yiiaseModel;
class Three extends Model{
public $name;
public $email;
public function rules()
{
return [
[['name','email'],'required'],
['email','email'],
];
}
}
定一个视图文件 views/site/three.php
<?php
use yiihelpersHtml;
use yiiwidgetsActiveForm;
?>
<?php
if(Yii::$app->session->hasFlash('success')){
echo Yii::$app->session->getFlash('success');
}
?>
<?php $form = ActiveForm::begin();?>
<?= $form->field($model,'name');?>
<?= $form->field($model,'email');?>
<?= Html::submitButton('submit',['class'=>'btn btn-success']);?>
~
注意 commit
没反应的情况和 model中的rule格式有关系浪费我半小时