Yii Tutorial on Yii HTML Forms

when a form is based upon a model, the common way of creating this form in yii is via the yii\widgets\activeform class. in most cases, a form has a corresponding model which is used for data validation. if the model represents data from a database, then the model should be derived from the activerecord class. if the model captures arbitrary input, it should be derived from the yii\base\model class.

let us create a registration form.

step 1 − inside the models folder, create a file called registrationform.php with the following code.

<?php
   namespace app\models;
   use yii;
   use yii\base\model;
   class registrationform extends model {
      public $username;
      public $password;
      public $email;
      public $subscriptions;
      public $photos;
      /**
      * @return array customized attribute labels
      */
      public function attributelabels() {
         return [
            'username' => 'username',
            'password' => 'password',
            'email' => 'email',
            'subscriptions' => 'subscriptions',
            'photos' => 'photos',
         ];
      }
   }
?>

we have declared a model for our registration form with five properties − username, password, email, subscriptions, and photos.

step 2 − to display this form, add the actionregistration method to the sitecontroller.

public function actionregistration() {
   $mregistration = new registrationform();
   return $this->render('registration', ['model' => $mregistration]);
}

we create an instance of the registrationform and pass it to the registration view. now, it is time to create a view.

step 3 − inside the views/site folder, add a file called registration.php with the following code.

<?php
   use yii\bootstrap\activeform;
   use yii\bootstrap\html;
?>
<div class = "row">
   <div class = "col-lg-5">
      <?php $form = activeform::begin(['id' => 'registration-form']); ?>
      <?= $form->field($model, 'username') ?>
      <?= $form->field($model, 'password')->passwordinput() ?>
      <?= $form->field($model, 'email')->input('email') ?>
      <?= $form->field($model, 'photos[]')->fileinput(['multiple'=>'multiple']) ?>
      <?= $form->field($model, 'subscriptions[]')->checkboxlist(['a' => 'item a',
         'b' => 'item b', 'c' => 'item c']) ?>
      <div class = "form-group">
         <?= html::submitbutton('submit', ['class' => 'btn btn-primary',
            'name' => 'registration-button']) ?>
      </div>
      <?php activeform::end(); ?>
   </div>
</div>

we observe the following −

  • the activeform::begin() function marks the beginning of the form. all the code between activeform::begin() and activeform::end() functions will be wrapped within the form tag.

  • to create a field in the form you should call the activeform::field() method. it creates all the input and label tags. input names are determined automatically.

  • for example, the password attribute will be registrationform[password]. if you want an attribute to take an array, you should append [ ] to the attribute name.

step 4 − if you go to the address bar of the web browser and type http://localhost:8080/index.php?r=site/registration, you will see our form.

registration