Freelance Jobs Freelance Jobs

Monday, April 26, 2010

Validation in CakePHP

In a website, some time user need to insert data into database. User can insert inappropriate data to database, different from the database data type or requirement of the site. So, we need to use validate data before inserting it into database.
Suppose in user registration form, there is field like user name, password, confirm password, first name, last name, email id. User name require only alphanumeric character and not empty field, email should be valid email address and not empty field, password should be at least 8 character and not empty field, confirm password should match with the password field, first name and last name should be alpha character and not empty field.
If we will not validate data according to condition of certain field, then whatever user insert, it will store into database. This is not a good programming practice.

Validation is require to check user's data before storing in database. For validation, we check user's data according to condition specific to field.

As we know, cakephp is a framework of php which is a model-view-controller architecture. If you don't have basic knowledge of cakephp, first you have to learn about it.
In cakephp, we use validate variable to set the condition for validating user's data. We write this variable in our model. Suppose we want to validate user registration form, then there will be user model, users controller, registration view file in our cake php.


In user model

var $validate=arra('uname'=>array('notEmpty'=>array(
'rule' => 'notEmpty',
'required' => true,
'message'=>'could not be empty',
'last' => true
),
'alphaNumeric'=>array(
'rule' => 'alphaNumeric',
'required' => true,
'message'=>'only alpha and number are allowed',
'last' => true
),
),
'passwd'=>array('notEmpty' => array(
'rule' => 'notEmpty',
'required' => true,
'message'=>'could not be empty',
'last' => true
)),
'email'=>array('notEmpty' => array(
'rule' => 'notEmpty',
'required' => true,
'message'=>'could not be empty',
'last' => true
)),
'fname'=>array('notEmpty' => array(
'rule' => 'notEmpty',
'required' => true,
'message'=>'could not be empty',
'last' => true
)),
'lname'=>array('notEmpty' => array(
'rule' => 'notEmpty',
'required' => true,
'message'=>'could not be empty',
'last' => true
)),
);




In users controller, register function

function register() {

if(isset($this->data))
{
$this->User->set($this->data);

/*$this->User->validates() use to validate user's data according to validate variable in user model*/
if($this->User->validates())
{
$this->User->save($this->data);
$this->Session->setFlash('Data has been saved');
$this->autoRender=false;
$this->redirect('index');
}
else
{
$this->set('errorMessage','Please correct below error');

}
}
}


Register view

$form->create('User',array('action'=>'register'));
print ""; print $form->label('User Name'); print $form->text('uname',true).$form->error('uname'); print $form->label('Password'); print $form->password('passwd').$form->error('passwd'); print $form->label('Email'); print $form->text('email').$form->error('email'); print $form->label('First Name'); print $form->text('fname').$form->error('fname') print $form->label('Last Name'); print $form->text('lname').$form->error('lname'); print $form->end('Register'); ?>


$form->error('uname') is used to display error in the output. If we are using $form->input then we don't need to use this variable to show error. But if we are using $form->text, then we need to use $form->error to show the error in the output.
In $form->error('fieldname'), fieldname is your database field name as model is automatically connected to database table in cakephp.

No comments:

Post a Comment