Book Image

Instant Building Multi-Page Forms with Yii How-to

By : Uday Sawant
Book Image

Instant Building Multi-Page Forms with Yii How-to

By: Uday Sawant

Overview of this book

Yii is a high-performance PHP framework for rapid web application development. It is well-designed, well-supported, easy to learn, and easy to maintain. Forms are the basic method used to collect information on the Internet, and in cases like user registration and payment process, there's a lot of data to be presented and the lists can get lengthy. Instant Building Multi-Page Forms with Yii is a simple and descriptive how-to that provides step-by-step recipes to help you in converting your lengthy forms into short, interactive forms. It will guide you through the inbuilt features of Yii framework to help you with this complex job. The book starts with the basics of Yii and will guide you through creating simple forms before then converting your simple forms into multi-page forms. It will use the capabilities of Yii to ease the tasks required to do this. Yii eases your work by providing options like database connectivity. We will also take a look at the automated code generation capabilities of Yii in generating forms and necessary CRUD operations, as well as looking at how to divide a single long form into multiple views with same controller/action, and then add form validations and AJAX support to the forms. We will also cover the upload process and how to customise Yii's looks.
Table of Contents (7 chapters)

Using the Gii tool (Simple)


In this recipe we will take a look at the graphical tool Gii. With the Gii tool, we can generate the following:

  • Controller

  • CRUD

  • Form

  • Model

  • Module

Getting ready

Set up a Yii environment and create a new web application. Set up the database connection for your database server.

How to do it...

We first need to create a database table.

  1. Create a database table user with the following code. This will hold all the necessary information for user registration.

    CREATE TABLE IF NOT EXISTS 'user' (
      'id' int(11) NOT NULL AUTO_INCREMENT,
      'first_name' varchar(50) NOT NULL,
      'last_name' varchar(50) NOT NULL,
      'gender' enum('male','female') NOT NULL,
      'dob' date NOT NULL,
      'address_1' varchar(255) NOT NULL,
      'address_2' varchar(255) DEFAULT NULL,
      'city' varchar(50) NOT NULL,
      'state' varchar(50) NOT NULL,
      'country' varchar(50) NOT NULL,
      'phone_number_1' varchar(50) NOT NULL,
      'phone_number_2' varchar(50) DEFAULT NULL,
      'email_1' varchar(255) NOT NULL,
      'email_2' varchar(255) DEFAULT NULL,
      'created' timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP 
      ON UPDATE CURRENT_TIMESTAMP,
      'modified' timestamp NOT NULL DEFAULT '0000-00-0000:00:00',
      PRIMARY KEY ('id')
    ) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;
  2. Next, enable the Gii tool. The Gii tool is related to the database system of our application. So, for security reasons this tool is disabled by default. We need to enable it from main.php.

    'modules'=>array(
      // uncomment the following to enable the Gii tool
      /*
      'gii'=>array(
        'class'=>'system.gii.GiiModule',
        'password'=>'root',
        // If removed, Gii defaults to localhost only. Editcarefully to taste.
        'ipFilters'=>array('127.0.0.1','::1'),
      ),  */  
    ),
  3. Uncomment the block to enable the Gii tool and set your password in place, and then you are done. The ipFilters line is used to restrict the access of the Gii tool to certain IP addresses only; in this case, it's localhost (127.0.0.1 OR ::1).

  4. We need to create models and controllers. To log in to the Gii tool, enter http://localhost/sampleapp/index.php?r=gii in your address bar. You will be provided with the login screen. Enter your password to log in.

How it works...

After you log in, you can see all the tasks that can be done with the help of the Gii tool, as shown in the following screenshot:

The tasks shown in the previous screenshot are explained here:

  • Controller Generator: It allows you to quickly generate a new controller class with one or more actions and the respective views. The default base class used is Controller.

  • CRUD Generator: It generates a controller and views that implement CRUD operations for a specified model.

  • Form Generator: It can be used to generate a view with form elements for a specified model.

  • Model Generator: It generates a model class for a specified database table. The base class used is CActiveRecord.

  • Module Generator: It provides a base code required by the Yii module

All these generators use the templates under the gii/generator folder. You can always customize them or use your own templates to modify the code according to your requirements.

Let's generate a model class for our table user as shown in the following screenshot:

Fill in the Table Name field and the Model Class field will be automatically populated; change it if required. Now click on Preview. If the database connection is properly set and Yii finds the table user, it will create the preview of a model file.

Click on the filename to preview the file or click on the Generate button to generate the model class. The new class will be stored in the protected/models folder.

The model class has some automatically generated functions such as tableName(), which returns the name of the table. The function rules() is used to specify the validation rules, the function relations() is used to specify the relations between two or more model classes, the function attributeLabels specifies the names for the attribute/columns in the database table, and the function search() sets the criteria for searching through the table data.

In the same way, we can generate the CRUD (Create Retrieve Update Delete) operations for the model user. Type the name of the model class (User) and click on the Preview button. A list of files to be created will appear, as shown in the following screenshot:

Click on the Generate button to generate files. UserController will be generated under protected/controllers and you can view the files under protected/view/user.

The generated controller contains some important functions, such as accessRules(), which is used to specify access control (user-level access) for each action in this controller, and filters(), which specifies the code to be executed before and/or after action execution. filters() can include access-control filters, performance filters, and other user-defined filters.

There's more...

To know more about filters(), visit the website http://www.yiiframework.com/doc/guide/1.1/en/basics.controller#filter.