English 中文(简体)
Yii Tutorial

Yii Useful Resources

Selected Reading

Yii - HTML Forms
  • 时间:2024-12-22

Yii - HTML Forms


Previous Page Next Page  

When a form is based upon a model, the common way of creating this form in Yii is via the yiiwidgetsActiveForm class. In most cases, a form has a corresponding model which is used for data vapdation. 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 yiiaseModel 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 appmodels;
   use Yii;
   use yiiaseModel;
   class RegistrationForm extends Model {
      pubpc $username;
      pubpc $password;
      pubpc $email;
      pubpc $subscriptions;
      pubpc $photos;
      /**
      * @return array customized attribute labels
      */
      pubpc 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.

pubpc 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 yiiootstrapActiveForm;
   use yiiootstrapHtml;
?>
<span class = "row">
   <span 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 ]) ?>
      <span class = "form-group">
         <?= Html::submitButton( Submit , [ class  =>  btn btn-primary ,
             name  =>  registration-button ]) ?>
      </span>
      <?php ActiveForm::end(); ?>
   </span>
</span>

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 Advertisements