Yii2 form field

Author: Rafal Marguzewicz
Published:
Categories: Yii2
Tags:


yii2 logo
This description is for Yii2 forms with model.
Form can have many type of fields. e.g:

  1. Field input (single-line)
  2. Field textarea (multi-line)
  3. Field radio (one choice)
  4. Field checkbox (one or several selection)
  5. Field select/dropDownList (one or several selection)
  6. Field hidden
  7. Field upload file
  8. Field password
  9. Field with a choice of type HTML5
    • text
    • rmail
    • password
    • date
    • number
    • Address URL
    • phone number
    • color
    • range

Additional options fields of the form:

  • Change field label (instead assigned from the model)
  • Additional tags (e.g. class or ID)
  • Default value
  • Checked or selected item of list

Class responsible for creating forms is yii\widgets\ActiveForm that generate fields by class yii\widgets\ActiveField.

If you need additional features that give bootstrap theme, you can create form by class yii\bootstrap\ActiveForm that generate field by class yii\bootstrap\ActiveField

To add a form to view, first we create a wrapper object instance ActiveForm

<?php
use yii\widgets\ActiveForm; // or yii\bootstrap\ActiveForm

$form = ActiveForm::begin() ?>
// there we add code render elements HTML
<?php ActiveForm::end() ?>

We can begin add elements of forms.

1. Field input (single-line)

 $form->field($model,  'name_field')->textInput();

2. Field textarea (multi-line)

$form->field($model,  'name_field')->textArea();

3. Field radio (one choice)

<?=$form->field($model, 'attribute_name')->radioList([
    1 => 'option 1', 
    2 => 'option 2'
]); ?>

4. Field checkbox (one or several selection)

<?= $form->field($model, 'attribute_name')->checkboxList([
    1 => 'option 1', 
    2 => 'option 2'
]); ?>

If you need single checkbox (e.g confirm regulations)

<?= $form->field($model, 'attribute_name')->checkbox() ?>

5. Field select/dropDownList (one or several selection)

<?= $form->field($model, 'attribute_name')->dropDownList() ?>

6. Field hidden

<?= $form->field($model, 'attribute_name')->hiddenInput() ?>

7. Field upload file

<?= $form->field($model, 'attribute_name')->fileInput() ?>

8. Field password

<?= $form->field($model, 'attribute_name')->passwordInput() ?>

9. Field with a choice of type HTML5

  1. Text: $form->field($model, 'attribute_name')->input('text')
  2. Email: $form->field($model, 'attribute_name')->input('email')
  3. Password: $form->field($model, 'attribute_name')->input('password')
  4. Date: $form->field($model, 'attribute_name')->input('date')
  5. Number: $form->field($model, 'attribute_name')->input('number')
  6. Adres URL: $form->field($model, 'attribute_name')->input('url')
  7. Phone number: $form->field($model, 'attribute_name')->input('tel')
  8. Color: $form->field($model, 'attribute_name')->input('color')
  9. Range: $form->field($model, 'attribute_name')->input('range')

Change field label (instead assigned from the model)

 $form->field($model, 'name_field')->textInput()->label('another label');

Additional tags (e.g. class or ID)

 $form->field($model, 'name_field')->textInput([
'class' => 'input-sm', 'placeholder' => 'text ext',
]);

Default value

 $form->field($model, 'name_field')->textInput(['value' => 'text default']);

Checked or selected item of list<

<?php
$model->attribute_name = 1;
echo $form->field($model, 'attribute_name')->checkboxList([
    1 => 'opcja 1', 
    2 => 'opcja 2'
]); ?>
Yii2 form field
4.9 / 11 votes


Popular search terms:
| | |

Your email address will not be published. Required fields are marked *

Name *
Email *

Read previous post:
Yii2 tests: Codeception configuration and first test

This tutorial is for Yii2 Basic Application Template. Yii2 especially the version 2.0.10+ help to begin tests with codeception. If...

Close