yii2.0 Activeform表单部分组件使用方法 [ 2.0 版本 ]

文本框:textInput();
密码框:passwordInput();
单选框:radio(),radioList();
复选框:checkbox(),checkboxList();
下拉框:dropDownList();
隐藏域:hiddenInput();
文本域:textarea(['rows'=>3]);
文件上传:fileInput();
提交按钮:submitButton();
重置按钮:resetButtun();

<?php $form = ActiveForm::begin(['action' => ['test/getpost'],'method'=>'post',]); ?> //提交的控制器
 
<?= $form->field($model, 'username')->textInput(['maxlength' => 20])->label('起的名字') ?>

<?= $form->field($model, 'password')->passwordInput(['maxlength' => 20])->label('起的名字') ?>

<?= $form->field($model, 'sex')->radioList(['1'=>'男','0'=>'女'])->label('起的名字') ?>

<?= $form->field($model, 'edu')->dropDownList(['1'=>'大学','2'=>'高中','3'=>'初中'], ['prompt'=>'请选择','style'=>'width:120px'])->label('起的名字') ?>

<?= $form->field($model, 'file')->fileInput()->label('起的名字') ?>

<?= $form->field($model, 'hobby[]')->checkboxList(['0'=>'篮球','1'=>'足球','2'=>'羽毛球','3'=>'乒乓球'])->label('起的名字') ?>

<?= $form->field($model, 'info')->textarea(['rows'=>3])->label('起的名字') ?>

<?= $form->field($model, 'userid')->hiddenInput(['value'=>3])->label('起的名字') ?>

<?= Html::submitButton('提交', ['class'=>'btn btn-primary','name' =>'submit-button']) ?>
   
<?= Html::resetButton('重置', ['class'=>'btn btn-primary','name' =>'submit-button']) ?>

<?php ActiveForm::end(); ?>

 控制器里接值
/**
     * Creates a new User model.
     * If creation is successful, the browser will be redirected to the 'view' page.
     * @return mixed
     */
    public function actionCreate()
    {
        $model = new User(); //实例化model

        if ($model->load(Yii::$app->request->post())) {
           $re=\Yii::$app->request->post();
           $sex=implode(',',$re['User']['sex']);//把数组转换字符串
           $re['User']['sex']=$sex;//重新赋值数组
           $model->name=$re['User']['name'];
           $model->pwd=$re['User']['pwd'];
           $model->sex=$re['User']['sex'];
           $model->save();//入库保存
            return $this->redirect(['view', 'id' => $model->id]);
        } else {
            return $this->render('create', [
                'model' => $model,
            ]);
        }
    }


注:本文由王智磊(王大宝儿)整理编写,也参考借鉴了很多大神的笔记,分享代码,分享成功,欢迎各位交流和转载,转载请注明出处(博客园:王大宝儿)http://www.cnblogs.com/wangzhilei/


posted @ 2017-03-10 18:58  王大宝儿  阅读(530)  评论(0编辑  收藏  举报