How to create forms in yii 2

Rakesh Sharma      4 Comments  

How to create forms in yii 2 ActiveForm

How to create forms in yii 2

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 

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.


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.


Creating Textarea fields:

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


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( 

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( 

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.


Hidden input Field:

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

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

The basic things i have shared that How to create forms in yii 2, For more information about creating forms follow Official Yii site

  • Dipanshu Mahajan

    How to generate checkboxlist with by default selected in yii 2.0

  • Dung Phan

    Hi sharma,
    i have problem with active field label. now when error message return, i want to embed it with label tag.Example
    Username that data-error contain error message.How do i change it, thank

  • David Connelly

    Nice tutorial. Good work!