最新消息: 新版网站上线了!!!

yii2常用表单处理方法

创建一个表单须加载 ‘yii\widgets\ActiveForm’ 和 ‘yii\helpers\Html’ 
ActiveForm::begin() - creates a form instance and  beginning of the form.
ActiveForm::begin() and ActiveForm::end() - All of the content placed between this.

使用别名空间 namespace 用 ActiveForm

<?php
use yii\helpers\Html;
use yii\widgets\ActiveForm;
?>

“的ActiveForm'命名空间是非常重要的创造一个积极的形式和”HTML“命名空间是显示不同的HTML输入字段非常有用。

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();
?>

表单上传的方法

使用输入文本框

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

 

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复选

 

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

Radio Button Field

 

<!-- 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'); ?>

单选按扭

 

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

ListBox 下拉框

 

<!-- 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'); ?>

dropDownList() function 使用下拉菜单

//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...']);>

提交按扭

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

转载请注明:谷谷点程序 » yii2常用表单处理方法