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)

Validating forms (Intermediate)


In this recipe we will look at the data-validation options provided by Yii.

Getting ready

We'll use the form we developed in the Building multipage forms recipe.

How to do it...

We have fields such as First Name, Last Name, Gender, Dob (date of birth), Phone Number 1, Phone Number 2, Email 1, and Email 2. Let's add data validation for these fields. First name and Last Name will be text-only fields, Gender will either be male or female, and Dob will be a date string.

Open a User model from the protected/models folder. Look for the function named rules(). Following are the rules created for the Building multipage forms recipe:

public function rules()
{
  return array(
    array('first_name, last_name, gender, dob', 
    'required', 'on'=>'page1'
  ),
  array('address_1, city, state, country', 
    'required', 'on'=>'page2'
  ),
  array('phone_number_1, email_1',  
    'required', 'on'=>'page3'
  ),
  array('first_name, last_name, city, state, country, 
    phone_number_1, phone_number_2', 
    'length', 'max'=>50
  ),
  array('gender', 'length', 'max'=>6),
  array('address_1, address_2, email_1, email_2',
    'length', 'max'=>255
  ),
  array('modified, dob', 'safe'),
  array('first_name, last_name, gender, dob, 
    address_1, address_2, city, state, 
    country, phone_number_1, phone_number_2, email_1,
    email_2, created, modified', 
    'safe', 'on'=>'search'
  ),
}

This function defines the validation rules used by the Yii forms. The Gii tool has already created some simple rules by reading the definition of the user table structure. For example, the maximum length for name, City, State, and so on is 50. The field's First Name, Last Name, Gender, Dob, Address 1, Address 2, and so on are the fields required while submitting the form. Let's customise the rules as per our requirements.

  1. To generate the first_name, last_name, city, and state name strings:

    array('first_name, last_name, city, state, country', 
      'type', 'type'=>'string'
    ),
  2. To make phone numbers numeric, add the following line to the rules array:

    array('phone_number_1, phone_number_2', 'numeric'),
  3. To check if the e-mail address is a valid address, use this:

    array('email_1, email_2', 'email'),
  4. To limit the values for gender, use this:

    array('gender','in','range'=>array('male','female'),
      'allowEmpty'=>false
    ),
  5. To check Dob (date of birth) for a valid date:

    array('dob', 'date', 'format'=>'mm-dd-yyyy'),

Yii provides a range of validation options. For example:

  • boolean: It checks for Boolean values; that is, true(1) or false(0)

  • compare: It compares the values against the given constant

  • captcha: For captcha code validation

  • default: To set the default values if the field is empty

  • file: To check the uploaded file's type, size, and number of files

There are many other options too.

How it works...

The rules() function returns a set of rules in a main array, with each rule specified in its separate array for one or more attributes. These rules are used by the validate() method of a model to determine validation of data on the server side. The save() method internally calls this validation and requires it to succeed before saving the record.

For client-side validation, Yii sends additional JavaScript with page contents that contain validation rules coded in JavaScript. With the following code, we can set the trigger to call client-side validation:

<?php $form=$this->beginWidget('CActiveForm', array(
  'id'=>'user-form',
  'enableAjaxValidation'=>false,
  'clientOptions'=>array(
    'validateOnSubmit'=>true,
    'validateOnChange'=>true,
  ),

The validateOnSubmit method calls the client-side form field validation before actually submitting a form (on the click of a submit button), while validateOnChange triggers field validation on the onchange event of the respective field.