Yii2 表单
来源:互联网 发布:.com.au域名注册 编辑:程序博客网 时间:2024/04/29 19:13
参考
http://www.yiiframework.com/doc-2.0/guide-input-forms.html
结构
- <?php
- namespace app\models\test;
- use yii\db\ActiveRecord;
- class Dynasty extends ActiveRecord //或者 \yii\base\Model
- {
- public $username;
- public $password;
- }
- <?php
- namespace app\controllers;
- use yii\web\Controller;
- use app\models\test\Dynasty;
- class TestController extends Controller
- {
- public function actionIndex()
- {
- return $this->render('index', [
- 'model' => new Dynasty(),
- ]);
- }
- }
- <?php
- use yii\helpers\Html;
- use yii\widgets\ActiveForm;
- ?>
- <?php $form = ActiveForm::begin(['id' => 'login-form','options' => ['class' => 'class_name'],'action'=>'test/index','method'=>'get',]); ?>
- <?= $form->field($model, 'username') ?>
- <?= $form->field($model, 'password')->passwordInput() ?>
- <div class="form-group">
- <?= Html::submitButton('Login') ?>
- </div>
- <?php ActiveForm::end(); ?>
规则
- 密码
- <?= $form->field($model, 'password')->passwordInput() ?>
- 标签与提示
- <?= $form->field($model, 'username')->textInput()->hint('请输入你的用户名')->label('用户名') ?>
- <?= $form->field($model, 'username[]',['inputOptions'=>['value'=>'abc','class'=>'form-control']]) ?>//默认值
- 邮箱
- <?= $form->field($model, 'username')->input('email') ?>
- 上传
- <?= $form->field($model, 'username')->fileInput(['multiple'=>'multiple']) ?>
- 多选列表
- <?= $form->field($model, 'username[]')->checkboxList(['a' => '多选a', 'b' => '多选b', 'c' => '多选c']) ?>
- 单个选择框
- <?= $form->field($model, 'username')->checkbox([],false)->label('已审核') ?> ?>
- 下拉列表
- <?= $form->field($model, 'username[]')->dropDownList(['a' => '多选a', 'b' => '多选b', 'c' => '多选c']) ?>
- <?= $form->field($model, 'username[]')->dropDownList(['a' => '多选a', 'b' => '多选b', 'c' => '多选c'], ['prompt' => '多选b']) ?>
- 隐藏框
- <?= $form->field($model, 'username')->hiddenInput(['1']) ?>
- ListBox
- <?= $form->field($model, 'username[]')->listBox(['a' => '多选a', 'b' => '多选b', 'c' => '多选c']) ?>
- 单选列表
- <?= $form->field($model, 'username[]')->radioList(['a' => '单选a', 'b' => '单选b', 'c' => '单选c']) ?>
- 多行文本
- <?= $form->field($model, 'username')->textarea() ?>
- widget扩展
- <?= $form->field($model, 'username')->widget(\yii\widgets\MaskedInput::className(), ['mask' => '9999/99/99',]); ?>
验证
- <?php
- namespace app\models\test;
- use yii\db\ActiveRecord;
- class Dynasty extends ActiveRecord
- {
- public $username;
- public $password;
- public $date;
- public function rules()
- {
- return [
- ['username', 'required', 'message' => '用户名不能为空.'],
- ['username', 'length', 'min'=>3, 'max'=>12],
- ['date','default','value'=>date('Y-m-d H:i:s')],
- ['verifyCode', 'captcha'],//验证码
- ];
- }
- }
- 为空检测
- [['username', 'password'], 'required', 'message'=>'不能为空'],
- 邮箱检测
- ['username', 'email'],
- <?= $form->field($model, 'body')->textArea(['rows' => 6]) ?>
- <?= $form->field($model, 'verifyCode')->widget(Captcha::className(), [
- 'template' => '<div class="row"><div class="col-lg-3">{image}</div><div class="col-lg-6">{input}</div></div>',
- ]) ?>
- <div class="form-group">
- <?= Html::submitButton('Submit', ['class' => 'btn btn-primary', 'name' => 'contact-button']) ?>
- </div>
接收数据
- public function actionIndex()
- {
- $model = new Dynasty();
- if ($model->load(Yii::$app->request->post()) && $model->validate()) {
- echo $model->username;
- return $this->render('index', ['model' => $model]);
- }else{
- }
- }
上传
CONTROLLER
- <?php
- namespace app\controllers;
- use yii;
- use yii\web\Controller;
- use app\models\test\Dynasty;
- use yii\web\UploadedFile;
- class TestController extends Controller
- {
- public function actionIndex()
- {
- if (Yii::$app->request->isPost) {
- $model = new Dynasty();
- $model->file = UploadedFile::getInstance($model, 'file');
- if ($model->file && $model->validate()) {
- $model->file->saveAs('uploads/' . $model->file->baseName . '.' . $model->file->extension); //uploads 相对网站根目录
- }
- return $this->render('index', ['model' => $model]);
- }
MODEL
- <?php
- namespace app\models\test;
- //use yii\base\Model;
- use yii\db\ActiveRecord;
- use yii\web\UploadedFile;
- class Dynasty extends ActiveRecord//Model
- {
- public $username;
- public $password;
- public $file;
- public function rules()
- {
- return [
- ['file', 'file', 'extensions' => 'gif, jpg, png'],
- ];
- }
- }
VIEW
- <?php $form = ActiveForm::begin(['id' => 'login-form','options' => ['enctype' => 'multipart/form-data']]); ?>
- <?= $form->field($model, 'username') ?>
- <?= $form->field($model, 'file')->fileInput() ?>
- <?= $form->field($model, 'password')->passwordInput() ?>
- <div class="form-group">
- <?= Html::submitButton('Login') ?>
- </div>
- <?php ActiveForm::end(); ?>
0 0
- Yii2 表单
- Yii2 表单
- YII2表单
- yii2表单
- YII2常用表单元素
- yii2 表单使用方法
- yii2之登录表单
- yii2自定义form表单
- yii2 表单错误提示
- yii2表单学习
- yii2表单验证方法
- yii2表单使用
- yii2表单生成
- Yii2创建表单
- Yii2.0 一个表单
- Yii2框架表单
- yii2.0 使用表单
- yii2表单使用
- chromeV.48后版本移除方法SVGGraphicsElement.getTransformToElement后的处理
- 12C-OCP升级1z-060-018
- 数据结构与算法分析---表ADT
- Expander控件-左边导航栏
- transient ,序列化
- Yii2 表单
- 命名空间(像std那样定义属于自己的专属空间)
- 如何防止恶意点击手机短信验证码
- 自定义HTML中select控件
- 设置eclipse自动下载源码
- move函数的用法
- 获取WinSCP保存的VPS密码
- Meta标签中的format-detection
- Retrofit使用