Yii2.0 ActiveForm Input Fields

  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


‘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

 '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
field($model,'name'); ?>
//Format 2
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.

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

Password Input Field

//Format 1
field($model, 'password')->input('password') ?>
//Format 2
field($model, 'password')->passwordInput() ?>
//Format 3
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

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

field($model, 'uploadFile')->fileInput() ?>

MultiFile Upload

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


field($model, 'population')->checkbox(); ?>

field($model, 'population')->checkbox(array('label'=>'')); ?>

field($model, 'population')	->checkbox(array('label'=>''))
										->label('Gender'); ?>

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.

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.


field($model, 'gender')->radio(); ?>

field($model, 'gender')->radio(array('label'=>'')); ?>

field($model, 'gender')	->radio(array('label'=>''))
										->label('Gender'); ?>

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.

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


field($model, 'population')-> listBox(
			array('1'=>'1',2=>'2',3=>3,4=>4,5=>5),
			array('prompt'=>'Select')
			); ?>

field($model, 'population')-> listBox(
			array('1'=>'1',2=>'2',3=>3,4=>4,5=>5),
			array('prompt'=>'Select','size'=>3)
			); ?>

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 is used to create HTML ‘select’ tag input field.

//Format 1
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

 'btn btn-primary']) ;?>

你可能感兴趣的:(Php,开发)