Yii2.0 ActiveForm Input Fields

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

之前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

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/400256.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

【RK3399Pro学习笔记】十三、ROS中的坐标系管理系统

目录TF功能包能干什么&#xff1f;TF坐标变换如何实现&#xff1f;例程view_framestf_echorviz平台&#xff1a;华硕 Thinker Edge R 瑞芯微 RK3399Pro 固件版本&#xff1a;Tinker_Edge_R-Debian-Stretch-V1.0.4-20200615 记录自【古月居】古月ROS入门21讲 | 一学就会的ROS机…

本地搭建wp,更新升级时需要ftp的解决办法

https://jingyan.baidu.com/article/fd8044fa2e7af35031137af2.html 本地安装完mysql&#xff0c;php&#xff0c;apache后&#xff0c;进行了wordpress的安装。安装完wordpress&#xff0c;发现wordpress已经有了新版本&#xff0c;点击“立即更新”后却跳转到一个填写ftp地址…

数据结构与算法-python描述-单链表

# coding:utf-8# 单链表的相关操作&#xff1a; # is_empty() 链表是否为空 # length() 链表长度 # travel() 遍历整个链表 # add(item) 链表头部添加元素 # append(item) 链表尾部添加元素 # insert(pos, item) 指定位置添加元素 # remove(item) 删除节点 # search(item) 查找…

【RK3399Pro学习笔记】十四、ROS中tf坐标系广播与监听的编程实现

目录创建功能包如何实现一个tf广播器创建tf广播器代码&#xff08;C&#xff09;如何实现一个tf监听器创建tf监听器代码&#xff08;C&#xff09;配置编译规则编译并运行python编写程序turtle_tf_broadcaster.pyturtle_tf_listener.py运行平台&#xff1a;华硕 Thinker Edge R…

SCOM数据库整理索引计划出错

我们为了提高数据库的访问效率&#xff0c;我们需要对数据库做优化&#xff0c;那么在这里我是希望对我的SCOM的SQL进行索引的整理。当我们在SQL的维护计划中创建了针对SCOM数据库进行重新组织索引的计划后&#xff0c;发现运行总是会失败&#xff0c;这是为什么呢&#xff1f;…

PL/SQL之高级篇

原文地址&#xff1a;http://www.cnblogs.com/sin90lzc/archive/2012/08/30/2661117.html 参考文献&#xff1a;《Oracle完全学习手册》 1.概述 本文主要介绍PL/SQL中的有名程序块&#xff1a;存储过程、函数、包头/包体及触发器的使用。而这些的基础是PL/SQL无名块的编写&…

下拉四级联动插件

http://www.jq22.com/jquery-info6023

Charles使用

主要还是移动端的使用技巧 常规使用&#xff1a;同一个wifi&#xff0c;设备开启代理&#xff0c;地址写本机&#xff0c;端口看Charles中的设置 下面说点不怎么常用但是蛮有用的 1.https 这个应该是蛮容易遇到的&#xff0c;设置其实也是蛮简单的 先来看下&#xff0c;未设置之…

最短路打印路径

#include <iostream>#include<string.h>#include<stack>#define M 100#define N 100using namespace std;typedef struct node{ int matrix[N][M]; //邻接矩阵 int n; //顶点数 int e; //边数 }MGraph; boo…

JS实现的五级联动菜单效果完整实例

https://www.jb51.net/article/106525.htm 本文实例讲述了JS实现的五级联动菜单效果。分享给大家供大家参考&#xff0c;具体如下&#xff1a; js实现多级联动的方法很多&#xff0c;这里给出一种5级联动的例子&#xff0c;其实可以扩展成N级联动,在做项目的时候碰到了这样一…

【RK3399Pro学习笔记】十五、ROS中launch启动文件的使用方法

目录Launch文件语法<launch><launch><launch><node><node><node>参数设置<param>/<rosparam><param>/<rosparam><param>/<rosparam><arg><arg><arg><remap><remap><…

关于有多少个1的计算

1、题目 输入一个十进制的数&#xff0c;输出 &#xff08;1&#xff09;、给定n&#xff0c;求出从1到n的所有整数中1的个数。&#xff08;暂用用f(n)表示&#xff09; &#xff08;2&#xff09;、求满足nf(n)的最小整数&#xff08;1除外&#xff09;。 #include <ios…

ABP+AdminLTE+Bootstrap Table权限管理系统第八节--ABP错误机制及AbpSession相关

返回总目录:ABPAdminLTEBootstrap Table权限管理系统一期 上一节我们讲到登录逻辑,我做的登录逻辑很简单的,我们来看一下abp module-zero里面的登录代码. #region Login / Logoutpublic ActionResult Login(string returnUrl ""){if (string.IsNullOrWhiteSpace(ret…

JQuery DataTables Editor---只修改页面内容

https://www.cnblogs.com/zjf1987/p/Editor.html

关于JAVA_HOME, CLASSPATH和PATH的设置

http://bbs.csdn.net/topics/120079565 1、PATH&#xff0c;这个是给WINDOWS操作系统用的&#xff0c;告诉命令行里&#xff0c;执行的命令行工具在那里&#xff0c;比如java,javac这都是命令行工具,你在运行里随便敲一个命令&#xff0c;操作系统怎么就知道你这个命令对应的程…

P2327 [SCOI2005]扫雷

题目描述 输入输出格式 输入格式&#xff1a; 第一行为N&#xff0c;第二行有N个数&#xff0c;依次为第二列的格子中的数。&#xff08;1< N < 10000&#xff09; 输出格式&#xff1a; 一个数&#xff0c;即第一列中雷的摆放方案数。 输入输出样例 输入样例#1&#xff…

天津海运[600751]股票

2019独角兽企业重金招聘Python工程师标准>>> 天津海运[600751]股票 转载于:https://my.oschina.net/chworld/blog/425583

【RK3399Pro学习笔记】十六、ROS中的常用可视化工具

目录测试rqt_consolerqt_graphrqt_plotrqt_image_viewrqtrvizgazebo平台&#xff1a;华硕 Thinker Edge R 瑞芯微 RK3399Pro 固件版本&#xff1a;Tinker_Edge_R-Debian-Stretch-V1.0.4-20200615 记录自【古月居】古月ROS入门21讲 | 一学就会的ROS机器人入门教程 —— 古月居G…

datatables 基本增删改查(php)

http://www.datatables.club/example/user_share/basic_curd.html

基于leveldb,levigo做二次开发

Leveldb是一个C库&#xff0c;它有提供标准的C接口&#xff0c;头文件在include/leveldb/c.h中 levigo是leveldb 的一个go wrapper,它主要基于Leveldb中inlude/leveldb/c.h提供的方法做二次开发 开发过程&#xff0c;以添加一个简单的计算功能为例&#xff1a; 修改leveldb源码…