Yii2.0 一个表单

来源:互联网 发布:c语言多个随机数 编辑:程序博客网 时间:2024/05/16 08:05

第一步:创建一个模型类
在application\models\下创建模型类文件:
编写Entry.php模型类文件

<?phpnamespace app\models;use yii;use yii\base\Model;class EntryForm extends Model//model自动验证,创建接受模型类{    public $name;    public $email;    public function rules()    {        return[        [['name','email'],'required'],        ['email','email'],        ];    }}第二步:创建控制器conteroller文件在application\controllers下面:创建controller文件,IndexController.php并把结果渲染到模板文件<?phpnamespace app\controllers;use Yii;use yii\filters\AccessControl;use yii\web\Controller;use yii\filters\VerbFilter;use app\models\LoginForm;use app\models\ContactForm;use app\models\EntryForm;//注意引入model模型类文件class SiteController extends Controller{    /**     * @inheritdoc     */    public function behaviors()    {        return [            'access' => [                'class' => AccessControl::className(),                'only' => ['logout'],                'rules' => [                    [                        'actions' => ['logout'],                        'allow' => true,                        'roles' => ['@'],                    ],                ],            ],            'verbs' => [                'class' => VerbFilter::className(),                'actions' => [                    'logout' => ['post'],                ],            ],        ];    }    /**     * @inheritdoc     */    public function actions()    {        return [            'error' => [                'class' => 'yii\web\ErrorAction',            ],            'captcha' => [                'class' => 'yii\captcha\CaptchaAction',                'fixedVerifyCode' => YII_ENV_TEST ? 'testme' : null,            ],        ];    }    /**     * Displays homepage.     *     * @return string     */    public function actionIndex()    {        return $this->render('index');    }    /**     * Login action.     *     * @return string     */    public function actionLogin()    {        if (!Yii::$app->user->isGuest) {            return $this->goHome();        }        $model = new LoginForm();        if ($model->load(Yii::$app->request->post()) && $model->login()) {            return $this->goBack();        }        return $this->render('login', [            'model' => $model,        ]);    }    /**     * Logout action.     *     * @return string     */    public function actionLogout()    {        Yii::$app->user->logout();        return $this->goHome();    }    /**     * Displays contact page.     *     * @return string     */    public function actionContact()    {        $model = new ContactForm();        if ($model->load(Yii::$app->request->post()) && $model->contact(Yii::$app->params['adminEmail'])) {            Yii::$app->session->setFlash('contactFormSubmitted');            return $this->refresh();        }        return $this->render('contact', [            'model' => $model,        ]);    }    /**     * Displays about page.     *     * @return string     */    public function actionAbout()    {        return $this->render('about');    }    public function actionSay($message = 'Hello')    {        return $this->render('say',['message'=>$message]);    }    public function actionEntry(){        $model = new EntryForm();//实例化模型类;创建表单        if($model->load(yii::$app->request->post()) && $model->validate())        {//验证model收到的数据,调用model对象的方法;            return $this->render('entry-confirm',['model'=>$model]);        }        else        {//无论初始化显示显示数据验证错误            return $this->render('entry',['model'=>$model]);//把结果交给模板        }    }}第三步:创建视图文件<?phpuse yii\helpers\Html;use yii\widgets\ActiveForm;//引入小部件功能创建表单;?><?php $form = ActiveForm::begin();?>    <?= $form->field($model,'name') ?>    <?= $form->field($model,'email') ?>    <div class="form-group">        <?= Html::submitButton('submit',['class' => 'btn btn-primary']) ?>    </div>    <?php ActiveForm::end(); ?>confirm.php<?phpuse yii\helpers\Html;//使用继承过滤输出?><p>you have entered the following information;</p><ul>    <li><label>Name</label>:<?= Html::encode($model->name) ?></li>    <li><label>Email</label>:<?= Html::encode($model->email) ?></li></ul>
0 0
原创粉丝点击