In this recipe we will look at the data-validation options provided by Yii.
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.
To generate the
first_name
,last_name
,city
, andstate
name strings:array('first_name, last_name, city, state, country', 'type', 'type'=>'string' ),
To make phone numbers numeric, add the following line to the rules array:
array('phone_number_1, phone_number_2', 'numeric'),
To check if the e-mail address is a valid address, use this:
array('email_1, email_2', 'email'),
To limit the values for gender, use this:
array('gender','in','range'=>array('male','female'), 'allowEmpty'=>false ),
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
) orfalse
(0
)compare
: It compares the values against the given constantcaptcha
: For captcha code validationdefault
: To set the default values if the field is emptyfile
: To check the uploaded file's type, size, and number of files
There are many other options too.
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.
You can visit the following website for various validation rules: