参考
http://www.yiiframework.com/doc-2.0/guide-input-forms.html
结构
render('index', [
'model' => new Dynasty(),
]);
}
}
'login-form','options' => ['class' => 'class_name'],'action'=>'test/index','method'=>'get',]); ?>
= $form->field($model, 'username') ?>
= $form->field($model, 'password')->passwordInput() ?>
= Html::submitButton('Login') ?>
规则
密码
= $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(yiiwidgetsMaskedInput::className(), ['mask' => '9999/99/99',]); ?>
验证
'用户名不能为空.'],
['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' => '',
]) ?>
= Html::submitButton('Submit', ['class' => 'btn btn-primary', 'name' => 'contact-button']) ?>
接收数据
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
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
'gif, jpg, png'],
];
}
}
VIEW
'login-form','options' => ['enctype' => 'multipart/form-data']]); ?>
= $form->field($model, 'username') ?>
= $form->field($model, 'file')->fileInput() ?>
= $form->field($model, 'password')->passwordInput() ?>
= Html::submitButton('Login') ?>