zoukankan      html  css  js  c++  java
  • 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'=>'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/

    
    
  • 相关阅读:
    算法笔记:树、堆和图
    typora主题配置:公众号一键排版
    算法笔记:哈希表、映射和集合
    Linux Bash之getopt命令行参数解析
    linux上常用命令
    python的selenium如何下载pdf文件
    关于日语分词Japanese segmenter
    python 语种识别的几种方法
    anaconda的安装与使用
    如何去拆分句子
  • 原文地址:https://www.cnblogs.com/wangzhilei/p/6532680.html
Copyright © 2011-2022 走看看