Yii2.0 ActiveForm Input Fields
2015-09-10 15:48
701 查看
Use the namespace For ActiveForm
Active Form Begin And End
Text Input Field
TextArea Field
Password Input Field
HTML5 Email Input Field
File Upload
Checkbox Button Field
Checkbox List Input Field
Radio Button Field
Radio Button List Field
ListBox Field
dropDown List Input Field
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’ namespace is very important to create the a active form and ‘Html’ namespace is very useful to display the different html input fields.
Here we added active form with basic details like form id, class and enctype for file uploads.
Format 1 is a normal text input field. Format 2 is a text input field with hint, label.
The model attribute value will be used as the content in the textarea.
We added different type of password input field like password with hint, custom lable.
fileInput() function is used to create a file input fields and ‘multiple’ parameter is used to upload multiple file in single upload.
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
checkboxList() function is used to display the check box list using array of input argument values.
The model attribute value will be used to create the redio button.
The model attribute value will be used to create the redio button list.
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
dropDownList() function is used to create HTML ‘select’ tag input field.
Active Form Begin And End
Text Input Field
TextArea Field
Password Input Field
HTML5 Email Input Field
File Upload
Checkbox Button Field
Checkbox List Input Field
Radio Button Field
Radio Button List Field
ListBox Field
dropDown List Input Field
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
<?php use yii\helpers\Html; use yii\widgets\ActiveForm; ?>
‘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
<?php use yii\helpers\Html; use yii\widgets\ActiveForm; //$form = ActiveForm::begin(); //Default Active Form begin $form = ActiveForm::begin([ 'id' => 'active-form', 'options' => [ 'class' => 'form-horizontal', 'enctype' => 'multipart/form-data' ], ]) /* ADD FORM FIELDS */ ActiveForm::end(); ?>
Here we added active form with basic details like form id, class and enctype for file uploads.
Text Input Field
//Format 1 <?= $form->field($model,'name'); ?> //Format 2 <?= $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.<?= $form->field($model, 'desc')->textarea(); ?> <?= $form->field($model, 'desc')->textarea()->label('Description'); ?> <?= $form->field($model, 'desc')->textarea(array('rows'=>2,'cols'=>5)); ?>
Password Input Field
//Format 1 <?= $form->field($model, 'password')->input('password') ?> //Format 2 <?= $form->field($model, 'password')->passwordInput() ?> //Format 3 <?= $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
<?= $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
<?= $form->field($model, 'uploadFile')->fileInput() ?>
MultiFile Upload
<?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<!-- CHECKBOX BUTTON DEFAULT LABEL --> <?= $form->field($model, 'population')->checkbox(); ?> <!-- CHECKBOX BUTTON WITHOUT LABEL --> <?= $form->field($model, 'population')->checkbox(array('label'=>'')); ?> <!-- CHECKBOX BUTTON WITH CUSTOM LABEL --> <?= $form->field($model, 'population') ->checkbox(array('label'=>'')) ->label('Gender'); ?> <!-- CHECKBOX BUTTON WITH LABEL OPTIONS, DISABLED AND STYLE PROPERTIES --> <?= $form->field($model, 'population')->checkbox(array( 'label'=>'', 'labelOptions'=>array('style'=>'padding:5px;'), 'disabled'=>true )) ->label('Gender'); ?>
Checkbox List Input Field
checkboxList() function is used to display the check box list using array of input argument values.<?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.<!-- RADIO BUTTON DEFAULT LABEL --> <?= $form->field($model, 'gender')->radio(); ?> <!-- RADIO BUTTON WITHOUT LABEL --> <?= $form->field($model, 'gender')->radio(array('label'=>'')); ?> <!-- RADIO BUTTON WITH CUSTOM LABEL --> <?= $form->field($model, 'gender') ->radio(array('label'=>'')) ->label('Gender'); ?> <!-- RADIO BUTTON WITH LABEL OPTIONS --> <?= $form->field($model, 'gender')->radio(array( 'label'=>'', 'labelOptions'=>array('style'=>'padding:5px;'))) ->label('Gender'); ?>
Radio Button List Field
The model attribute value will be used to create the redio button list.<?= $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<!-- Listbox with prompt text --> <?= $form->field($model, 'population')-> listBox( array('1'=>'1',2=>'2',3=>3,4=>4,5=>5), array('prompt'=>'Select') ); ?> <!-- Listbox with size --> <?= $form->field($model, 'population')-> listBox( array('1'=>'1',2=>'2',3=>3,4=>4,5=>5), array('prompt'=>'Select','size'=>3) ); ?> <!-- Listbox with disabled, style properties --> <?= $form->field($model, 'population')-> listBox( array('1'=>'1',2=>'2',3=>3,4=>4,5=>5), array('disabled' => true,'style'=>'background:gray;color:#fff;')) ->label('Gender'); ?>
dropDown List Input Field
dropDownList() function is used to create HTML ‘select’ tag input field.//Format 1 <?php echo $form->field($model, 'name')->dropDownList(['a' => 'Item A', 'b' => 'Item B', 'c' => 'Item C']); ?> // Format 2 < echo $form->field($model, 'name')->dropDownList($listData, ['prompt'=>'Choose...']);>
Submit Button
<?= Html::submitButton('Submit', ['class'=> 'btn btn-primary']) ;?>
相关文章推荐
- php使用递归创建多级目录
- win7 64位旗舰版电脑中如何让EditPlus软件在保存文件时不生成bak文件?
- php对文本文件进行分页功能简单实现
- PHP笔记02变量的作用域
- PHP笔记01
- php 日期
- phpmyadmin 4 配置
- PHP下载封装
- DooDigestAuth php(后台)授权管理类 web浏览器授权
- 计算G711语音的打包长度和RTP里timestamp的增长量
- php读取excel文件的日期时间问题
- php正则判断字符串是否包含中文或特殊字符等
- PHP setcookie() 报 cannot modify header information 错误的解决方法
- 使用Composer管理PHP依赖关系
- phpstorm配置svn
- zimbra配置启用SMTP认证
- zimbra配置启用SMTP认证
- Mac 自带PHP 如何正常连接 PGSql ( 及编译 pgsql.so )
- 再说“php解决服务器美国时间的问题”
- phpcms企业会员组登录进入到企业会员商务中心的修改方法!