YII2.0使用ActiveForm表单
Controller控制器层代码
load(\Yii::$app->request->post()) && $model->validate())
{
echo "通过";
}
return $this->render('index',[
"model" => $model,
]);
}
}
VIEWS视图层代码
YII2.0使用ActiveForm
['log/login'], //提交地址(*可省略*)
'method'=>'post', //提交方法(*可省略默认POST*)
'id' => 'login-form', //设置ID属性
'options' => [
'class' => 'form-horizontal', //设置class属性
'enctype' => 'multipart/form-data' //文件上传时添加该编码
],
'fieldConfig' => [
'template' => '
'
], //设置模板的样式
]); ?>
= $form->field($model, 'username',['inputOptions' => ['placeholder'=>'请输入用户名','class' => 'ipt'],'template'=>'
'])->textInput(['maxlength' => 20,"style"=>"width:200px; height:30px;"]) ?>
密码
= $form->field($model, 'pwd')->label(false)->passwordInput(['maxlength' => 20,"style"=>"width:200px; height:30px;","placeholder"=>"请输入您的密码"]) ?>
= $form->field($model, 're_pwd')->passwordInput(['maxlength' => 20,"style"=>"width:200px; height:30px;","placeholder"=>"请输入您的密码"]) ?>
sex=1; echo $form->field($model, 'sex')->radioList(['1'=>'男','0'=>'女']) ?>
= $form->field($model, 'email')->textInput() ?>
= $form->field($model, 'school')->dropDownList(['1'=>'大学','2'=>'高中','3'=>'初中'], ['prompt'=>'请选择','style'=>'width:120px']) ?>
= $form->field($model, 'photo')->fileInput() ?>
= $form->field($model, 'hobby')->checkboxList(['0'=>'篮球','1'=>'足球','2'=>'羽毛球','3'=>'乒乓球']) ?>
= $form->field($model, 'remark')->textarea(['rows'=>3]) ?>
= $form->field($model, 'userid')->hiddenInput(['value'=>3])->label(false); ?>
= Html::submitButton('提交', ['class'=>'btn btn-primary','name' =>'submit-button']) ?>
= Html::resetButton('重置', ['class'=>'btn btn-primary','name' =>'submit-button']) ?>
MODELS层表单验证
"{attribute}不能为空"],
//验证用户唯一
['username', 'unique', 'targetClass' => '\frontend\models\User', 'message' => '用户名已存在.'],
//验证密码不一致
['re_pwd', 'compare', 'compareAttribute' => 'pwd', 'message' => '两次密码不一致'],
//验证字符串长度
[['username'],"string", "max"=>"10", "min"=>"5", "tooLong"=>"{attribute}不能大于10个字符", "tooShort"=>"{attribute}不能小于5个字符"],
//验证文件上传的格式
['photo','file',
'extensions'=>['jpg','png','gif'],'wrongExtension'=>'只能上传{extensions}类型文件!',
'maxSize'=>1024*1024*2, 'tooBig'=>'文件上传过大!',
'skipOnEmpty'=>false,'uploadRequired'=>'请上传文件!',
'message'=>'上传失败!'
]
//采用rules 规则验证
['email', 'email',"message"=>"{attribute}格式错误"],
//方法2:
//正则验证 ['tel','match','pattern'=>'/^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})?$/','message'=>"{attribute}邮箱输入有误."],
[['remark'], 'string', 'max' => 200],
];
}
/**
* @inheritdoc
*/
public function attributeLabels()
{
return [
'user_id' => '自增ID',
'username' => '用户名',
'pwd' => '密码',
're_pwd' => '请再次输入密码',
'sex' => '性别',
'photo' => '头像',
'email' => '邮箱',
'hobby' => '爱好',
'school' => '学校',
'remark' => '备注信息',
];
}
}
相关的学习网站推荐
http://phpxs.com/post/2327
http://phpxs.com/post/3441
http://phpxs.com/post/4378
http://phpxs.com/post/3443
http://kuitao8.com/20140425/2334.shtml
http://yiichina.com/tutorial/635
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
暂时没有评论,来抢沙发吧~