2019独角兽企业重金招聘Python工程师标准>>>

之前5月学习Yii2的时候发现的一个不错的博客内容,这里转载保存。

  1. Use the namespace For ActiveForm
  2. Active Form Begin And End
  3. Text Input Field
  4. TextArea Field
  5. Password Input Field
  6. HTML5 Email Input Field
  7. File Upload
  8. Checkbox Button Field
  9. Checkbox List Input Field
  10. Radio Button Field
  11. Radio Button List Field
  12. ListBox Field
  13. dropDown List Input Field
  14. Submit Button

‘yii\widgets\ActiveForm’ class is used to create a form and ‘yii\helpers\Html’ class is used to display the different type of HTML input fields like buttons, textbox, select box etc.

ActiveForm::begin() - creates a form instance and  beginning of the form.
ActiveForm::begin() and ActiveForm::end() - All of the content placed between this.

Use the namespace For ActiveForm

1 <?php
2 use yii\helpers\Html;
3 use yii\widgets\ActiveForm;
4 ?>

‘ActiveForm’ namespace is very important to create the a active form and ‘Html’ namespace is very useful to display the different html input fields.

Active Form Begin And End

01 <?php
02 use yii\helpers\Html;
03 use yii\widgets\ActiveForm;
04  
05 //$form = ActiveForm::begin(); //Default Active Form begin
06 $form = ActiveForm::begin([
07     'id' => 'active-form',
08     'options' => [
09                 'class' => 'form-horizontal',
10                 'enctype' => 'multipart/form-data'
11                 ],
12 ])
13 /* ADD FORM FIELDS */
14 ActiveForm::end();
15 ?>

Here we added active form with basic details like form id, class and enctype for file uploads.

Text Input Field

1 //Format 1
2 <?= $form->field($model,'name'); ?>
3 //Format 2
4 <?= $form->field($model, 'name')->textInput()->hint('Please enter your name')->label('Name') ?>

Format 1 is a normal text input field. Format 2 is a text input field with hint, label.

TextArea Field

The model attribute value will be used as the content in the textarea.

1 <?= $form->field($model, 'desc')->textarea(); ?>
2 <?= $form->field($model, 'desc')->textarea()->label('Description'); ?>
3 <?= $form->field($model, 'desc')->textarea(array('rows'=>2,'cols'=>5)); ?>

Password Input Field

1 //Format 1
2 <?= $form->field($model, 'password')->input('password') ?>
3 //Format 2
4 <?= $form->field($model, 'password')->passwordInput() ?>
5 //Format 3
6 <?= $form->field($model, 'password')->passwordInput()->hint('Password should be within A-Za-z0-9')->label('Password Hint') ?>

We added different type of password input field like password with hint, custom lable.

HTML5 Email Input Field

1 <?= $form->field($model, 'email')->input('email') ?>

File Upload

fileInput() function is used to create a file input fields and ‘multiple’ parameter is used to upload multiple file in single upload.

Single File Upload

1 <?= $form->field($model, 'uploadFile')->fileInput() ?>

MultiFile Upload

1 <?php echo $form->field($model, 'uploadFile[]')->fileInput(['multiple'=>'multiple']); ?>

Checkbox Button Field

Using below we can create the Checkbox base on model attribute of yii2.0 framework. We added the following options like custom label, disabled, style etc

01 <!-- CHECKBOX BUTTON DEFAULT LABEL -->
02 <?= $form->field($model, 'population')->checkbox(); ?>
03 <!-- CHECKBOX BUTTON WITHOUT LABEL -->
04 <?= $form->field($model, 'population')->checkbox(array('label'=>'')); ?>
05 <!-- CHECKBOX BUTTON WITH CUSTOM LABEL -->
06 <?= $form->field($model, 'population')    ->checkbox(array('label'=>''))
07                                         ->label('Gender'); ?>
08 <!-- CHECKBOX BUTTON WITH LABEL OPTIONS, DISABLED AND STYLE PROPERTIES -->
09 <?= $form->field($model, 'population')->checkbox(array(
10                                 'label'=>'',
11                                 'labelOptions'=>array('style'=>'padding:5px;'),
12                                 'disabled'=>true                            
13                                 ))
14                                 ->label('Gender'); ?>

Checkbox List Input Field

checkboxList() function is used to display the check box list using array of input argument values.

1 <?php echo $form->field($model, 'name[]')->checkboxList(['a' => 'Item A', 'b' => 'Item B', 'c' =>'Item C']); ?>

Radio Button Field

The model attribute value will be used to create the redio button.

01 <!-- RADIO BUTTON DEFAULT LABEL -->
02 <?= $form->field($model, 'gender')->radio(); ?>
03 <!-- RADIO BUTTON WITHOUT LABEL -->
04 <?= $form->field($model, 'gender')->radio(array('label'=>'')); ?>
05 <!-- RADIO BUTTON WITH CUSTOM LABEL -->
06 <?= $form->field($model, 'gender')    ->radio(array('label'=>''))
07                                         ->label('Gender'); ?>
08 <!-- RADIO BUTTON WITH LABEL OPTIONS -->
09 <?= $form->field($model, 'gender')->radio(array(
10                                 'label'=>'',
11                                 'labelOptions'=>array('style'=>'padding:5px;')))
12                                 ->label('Gender'); ?>

Radio Button List Field

The model attribute value will be used to create the redio button list.

1 <?= $form->field($model, 'population')->radioList(array('1'=>'One',2=>'Two')); ?>

ListBox Field

Using below we can create the list box base on model attribute of yii2.0 framework. We added the following options like prompt, size, disabled, style etc

01 <!-- Listbox with prompt text -->
02 <?= $form->field($model, 'population')-> listBox(
03             array('1'=>'1',2=>'2',3=>3,4=>4,5=>5),
04             array('prompt'=>'Select')
05             ); ?>
06 <!-- Listbox with size -->
07 <?= $form->field($model, 'population')-> listBox(
08             array('1'=>'1',2=>'2',3=>3,4=>4,5=>5),
09             array('prompt'=>'Select','size'=>3)
10             ); ?>
11 <!-- Listbox with disabled, style properties -->
12 <?= $form->field($model, 'population')-> listBox(
13             array('1'=>'1',2=>'2',3=>3,4=>4,5=>5),
14             array('disabled' => true,'style'=>'background:gray;color:#fff;'))
15             ->label('Gender'); ?>

dropDown List Input Field

dropDownList() function is used to create HTML ‘select’ tag input field.

1 //Format 1
2 <?php echo $form->field($model, 'name')->dropDownList(['a' => 'Item A', 'b' => 'Item B', 'c' => 'Item C']); ?>
3 // Format 2
4 < echo $form->field($model, 'name')->dropDownList($listData, ['prompt'=>'Choose...']);>

Submit Button

1 <?= Html::submitButton('Submit', ['class'=> 'btn btn-primary']) ;?>

转载于:https://my.oschina.net/kenblog/blog/424043

Yii2.0 ActiveForm Input Fields相关推荐

  1. php实现多条件查找分页,Yii2.0框架实现带分页的多条件搜索功能示例

    本文实例讲述了Yii2.0框架实现带分页的多条件搜索功能.分享给大家供大家参考,具体如下: 方法一 在控制器中 public function actionShow(){ $where['title' ...

  2. php表单yii2,yii2教程-ActiveForm表单组件

    简介 yii2中最常用的组件activeform,通过对activeform的灵活运用,能有效的提升开发效率,所以这个是不得不说的一个yii2组件,那么下面就来了解一下yii2.0的ActiveFor ...

  3. yii2.0使用ueditior完成上传单张,多张图片,上传视频等操作

    一.前言 由于工作需求需要集成富文本编辑器,本来是想要选用之前用过的WangEditor的,但是考虑到WangEditor还是比较小众,所以最终选择了没用过的Uedtor,这篇文章主要讲述了Yii2. ...

  4. yii2.0 下拉菜单

    第一种方法:ActiveForm 类的 dropDownList 方法(优点,默认使用yii的样式) 1.在控制器的方法里面 ,我们需要拿到数据,一定是 findAll() 或者是 all() 方法的 ...

  5. YII2.0电商平台开发笔记

    nginx+mysql+php+yiifromework+vim+composer(框架和扩展组件的开发) 前台:商品展示功能,用户购买流程: 后台:数据管理 phpStudy 安装 composer ...

  6. php yii2.0框架下载,yii2.0下载|yii2.0(php框架) v2.0.10官方版 附安装教程 - 121下载站...

    yii2.0是一款高性能的php框架,使用这个框架可以帮助用户快速开发出各类实用的网站应用,包括门户网站.论坛.内容管理系统(CMS).电子商务项目和RESTful Web服务等,它包含了丰富的组件和 ...

  7. php+yii手册下载,yii框架(含yii2.0中文手册)

    yii是BSD License下发布的一个开源项目,这意味着您能免费使用它开发开源或私有的Web应用程序,作为一款高性能,基于组件的php框架,因为框架结构和设计精巧的缓存支持,能够完成开发如门户网站 ...

  8. Nginx+Apache Yii2.0 配置方案

    最近用Yii2.0框架做了个小项目,虽然项目本身业务逻辑不复杂,但是由于本身业务逻辑的特殊性,在上午9点到12点之间系统访问量会突然上升(浏览量和用户上传文件量).导致系统单纯的部署在Apache下, ...

  9. Yii2.0 RESTful API 之版本控制

    Yii2.0 RESTful API 之版本控制 之前我写过两篇关于 Yii2.0 RESTful API 如何搭建,以及 认证 等处理,但是没有涉及到版本管理,今天就来谈谈版本管理如何实现. 索性就 ...

最新文章

  1. 你真的了解AI吗?AI将怎么改变我们的生活?
  2. Jmeter(一)http接口添加header和cookie --转载
  3. C++ : KMP 字符串匹配算法
  4. 你没有见过的 7 种 for 循环优化,超好用!
  5. Junit5集成到Maven工程
  6. excel快速填充_F4键,Excel中最强大的快捷键,没有之一
  7. 使用机器视觉模式识别屏幕
  8. linux块设备驱动编写,Linux内核学习笔记 -49 工程实践-编写块设备驱动的基础
  9. [BZOJ1968][AHOI2005]COMMON约数研究 数学
  10. Cocos2dx使用 TexturePacker导出的.plist
  11. css实现背景颜色半透明的两种方法
  12. 【网络技术联盟站】网络安全 | 瑞哥带你全方位解读防火墙技术!
  13. Android Srt和Ass字幕解析器
  14. java实验指导书 王立新,java实验指导书
  15. 关于MATLAB的saveas函数错误
  16. 计算机硬件知识应用,计算机硬件知识 (很详细)
  17. 数学分析第一型曲面积分2021.6.5
  18. 计算机xp系统如何更换桌面,xp系统修改桌面后重启电脑桌面又回到初始状态的处理教程...
  19. 从双倍数组中还原原数组
  20. 掉队的魅族还能和小米平起平坐吗?| 畅言

热门文章

  1. java读取gxk文件,Java中常见的IO流及其使用
  2. Qt Creator 窗体控件自适应窗口大小布局
  3. 运用递归将两个链表进行连接
  4. 函数sigsuspend
  5. 真香警告!2021Android高级面试题,挥泪整理面经
  6. 计算机房的英语用谐音怎么读,“人机对话”学英语 发音不准就过不了电脑关...
  7. ListView与.FindControl()方法的简单练习 #2 -- ItemUpdting事件中抓取「修改后」的值
  8. 在layui中使用 jquery 触发select 的 change事件无效
  9. jQuery事件整合
  10. 理论与哲学就是梳理无限感性经验和知性知识的工具