1.yii\basic\models\下新建一个UserForm.php
[code]
<?php
namespace app\models;
use yii\base\Model;
class UserForm extends Model
{
public $name;
public $email;
public function rules()
{
return [
[['name','email'],'required'],
['email','email'],
];
}
}
?>
[/code]
2.yii\basic\controllers\SiteController.php里面增加一个function
[code]
public function actionUser()
{
$model= new UserForm;
if($model->load(yii::$app->request->post()) && $model->validate())
{
echo "<div class='alert-success'>".Yii::$app->session->setFlash('success','ok,it is right!')."</div>";
}
// else
// {
return $this ->render('userForm',['model'=>$model]);
// }
}
[/code]
3.yii\basic\views\site\下新建userForm.php
[code]
<?php
use yii\helpers\Html;
use yii\widgets\ActiveForm;
?>
<?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'])?>
[/code]