- Gii – Generating Module
- Gii – Generating Controller
- Gii – Creating a Model
- Yii - Gii
- Yii - Localization
- Yii - Authorization
- Yii - Authentication
- Yii - Error Handling
- Yii - Logging
- Yii - Aliases
- Yii - Fragment Caching
- Yii - Caching
- Yii - Testing
- Yii - Fields
- Yii - RESTful APIs in Action
- Yii - RESTful APIs
- Yii - Theming
- Yii - Database Migration
- Yii - Active Record
- Yii - Query Builder
- Yii - Data Access Objects
- Yii - Database Access
- Yii - Dependency Injection
- Yii - Configurations
- Yii - Creating a Behavior
- Yii - Behaviors
- Yii - Creating Event
- Yii - Events
- Yii - GridView Widget
- Yii - ListView Widget
- Yii - Data Widgets
- Yii - Data Providers
- Yii - Properties
- Yii - Sorting
- Yii - Pagination
- Yii - Formatting
- Yii - Files Upload
- Yii - Using Cookies
- Yii - Cookies
- Yii - Using Flash Data
- Yii - Sessions
- Yii - AJAX Validation
- Yii - Ad Hoc Validation
- Yii - Validation
- Yii - HTML Forms
- Yii - Rules of URL
- Yii - URL Routing
- Yii - URL Formats
- Yii - Responses
- Yii - HTTP Requests
- Yii - Creating Extensions
- Yii - Extensions
- Yii - Asset Conversion
- Yii - Assets
- Yii - Layouts
- Yii - Views
- Yii - Modules
- Yii - Widgets
- Yii - Models
- Yii - Using Actions
- Yii - Using Controllers
- Yii - Controllers
- Yii - Entry Scripts
- Yii - Application Structure
- Yii - Create Page
- Yii - Installation
- Yii - Overview
- Yii - Home
Yii Useful Resources
Selected Reading
- Who is Who
- Computer Glossary
- HR Interview Questions
- Effective Resume Writing
- Questions and Answers
- UPSC IAS Exams Notes
Yii - HTML Forms
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.
Advertisements