In Yii2 we use the ‘yii\widgets\ActiveForm’ and ‘yii\helpers\Htm’ together to create forms.The former class is used to create a form and the later one is to render various form elements like textboxes,radio buttons,checkboxes etc. In this article we will explore how to create forms in yii 2 using activeform class.
First we need include the above two classes in our view file.

use yii\helpers\Html; 
use yii\widgets\ActiveForm;

Now we have included the necessary classes, we have to create the form instance and begin the form using ActiveForm::begin() method.In the same way we can close the form using ActiveForm::end(). Note that we can instantiate the form with several attributes.This can be done by passing parameters to the ActiveForm::begin() method.

use yii\helpers\Html; 
use yii\widgets\ActiveForm;

/* default initialization */

$form = ActiveForm::begin();

/* parameterized initialization */ 
$form = ActiveForm::begin([ 
‘id’ => ‘form_id’, 
‘options’ => [ 
‘class’ => ‘form_class’, 
‘enctype’ => ‘multipart/form-data’, 
], 
]); 
//render form elements here 
ActiveForm::end();

Now for creating various form elements we will be requiring the Model object which we have passed to the view while rendering it.

Creating Text inputs:

A simple text input filed can be created as shown below example.

$form->field($model,'fieldName');

To customize the hint and label fields we can use the following code below.

  $form->field($model,’fieldName’)->textInput()->hint(‘Hint Text’)->label(‘Label Name’);

Also the textInput() class accepts an array as a parameter.You can set the various html attributes of the element such as class,maxlength,minlength in the parameter.Details can be found form the Yii2’s class reference.

Also we can use the following code to generate a text field as well.

$form->field($model,’fieldName’)->input(‘text);

Creating Textarea fields:

In a similar manner we can create text areas as per the below code.

$form->field($model,'fieldName')->textarea(); 
$form->field($model,'fieldName')->textarea()->label('Label'); 
$form->field($model,'fieldName')->textarea(array('rows'=>2,'cols'=>5));

Creating password fields:

Password fields can be created using the following code:

//Format 1 
$form->field($model, ‘fieldName’)->input(‘password’); 
//Format 2 
$form->field($model, ‘fieldName’)->passwordInput(); 
//Format 3 
$form->field($model, ‘fieldName’)->passwordInput()->hint(‘Provide your hint here’)->label(‘Label’);

Email Input Field:

The following code generates a html email input field.

$form->field($model, 'fieldName')->input('email');

File Upload Field:

fileInput() function is used to create a file input field

$form->field($model, 'fieldName')->fileInput();

To create a multiupload field use ‘multiple’ parameter in fileInput() method.Please refer the code below for this.

$form->field($model, 'fieldName[]')->fileInput(['multiple'=>'multiple']);

Checkbox button Field:

$form->field($model, 'fieldName')->checkbox();

We can customize the label and other attributes by passing an array as an attribute to the checkbox() function.

$form->field($model, 'fieldName')->checkbox(array( 
'label'=>'', 
'labelOptions'=>array('style'=>'padding:5px;'), 
'disabled'=>true 
)) 
->label('Label');

Checkbox List Input Field:

Using checkboxList() method and passing an array as the argument to it we can create a checkbox list.The array will be rendered as the options for the checkbox list.

$form->field($model, 'fieldName[]')->checkboxList(['a' => 'Item A', 'b' => 'Item B', 'c' => 'Item C']);;

Radio button Field:

Much similar to the checkbox field we can create a radio button field as shown below:

//wihout any parameters

$form->field($model, 'fieldName')->radio();
//with parameter

$form->field($model, 'fieldName')->radio(array( 
'label'=>'', 
'labelOptions'=>array('style'=>'padding:5px;'), 
'disabled'=>true 
)) 
->label('Label');

Radio button list Field:

Using radioList() method we can create a radio button list, similarly we have created for the checkbox list.

$form->field($model, 'fieldName[]')->radioList(['a' => 'Item A', 'b' => 'Item B', 'c' => 'Item C']);

DropDown List Field:

dropDownList() function is used to create HTML ‘select’ tag input field.The options are passed as an array as an arguments.

$form->field($model, 'fieldName')->dropDownList(['a' => 'Item A', 'b' => 'Item B', 'c' => 'Item C']);

Hidden input Field:

To create a hidden input field we use hidden Input method.

$form->hiddenInput($model,'fieldName',array('value'=>value'));

Hidden input Field:

Finally the form submit button can be created using Html::submitButton() method.

Html::submitButton(‘Submit’, [‘class’=> ‘btn btn-primary’]);