Validation in cake php,server side validation in cake php,multiple validation in cakephp

Validation in cake php,server side validation in cake php,multiple validation in cakephp

Valation in cakephp, number validation,textarea validation,input field validation, email validation,is unique validation,multiple validation on same field. min length validation,max length validation.

step1 create register.ctp

<?php
Form->create('User',array('type'=>'file','novalidate'=>true,'inputDefaults'=>array()));
echo $this->Form->input('f_name',array('label'=>'First Name'));
echo $this->Form->input('l_name',array('label'=>'Last Name'));
echo $this->Form->input('email');
echo $this->Form->input('password');
?>

Form->textarea('address',array('escape'=>false,'label'=>'Address'));
echo $this->Form->input('mobile',array('label'=>'Mobile'));
echo $this->Form->submit();
echo $this->Form->end();
?>
validation1

validation3

‘novalidate’=>true is used to remove html5 validation.

step2:first create controller hare my controller UsersController.php

<?php /**  * Application level Controller  * 
 * This file is application-wide controller file. You can put all  
* application-wide controller-related methods here.  *  
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)  
* Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)  *  
* Licensed under The MIT License  * For full copyright and license information, please see the LICENSE.txt  
* Redistributions of files must retain the above copyright notice.  *  
* @copyright     Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org) 
 * @link          http://cakephp.org CakePHP(tm) Project  * @package       app.Controller  
* @since         CakePHP(tm) v 0.2.9 
 * @license       http://www.opensource.org/licenses/mit-license.php MIT License  
*/ App::uses('Controller', 'Controller'); 
/**  * Application Controller  *  
* Add your application-wide methods in the class below, your controllers 
 * will inherit them.  *  * @package		app.Controller  
* @link		http://book.cakephp.org/2.0/en/controllers.html#the-app-controller  
*/ 
class UsersController extends Controller { 	
public function login() 
{ 	
} 	
public function register()
 { 		
if ($this->request->is('post')) {
			$this->User->set($this->request->data);////////////////////////////////////set data to user model
			 if ($this->User->validates()) { //////////////////////////////////////////validation function in User.php model
			
					//$this->request->data['User']['password'] = AuthComponent::password($this->request->data['User']['password']);
					if ($this->User->save($this->request->data)) {
					$this->Session->setFlash('User created successfully');
					//$this->redirect(array('action' => 'login'));
					} else {
					$this->Session->setFlash('Error');
					}
				}
		}

	}

}

step4: now Create mode name User.php and create validates() funciton.

<?php 
/**  * 
Application model for CakePHP.  
*  * This file is application-wide model file. You can put all  
* application-wide model-related methods here.  * 
 * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)  
* Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)  * 
 * Licensed under The MIT License  
* For full copyright and license information, please see the LICENSE.txt  
* Redistributions of files must retain the above copyright notice.  *  
* @copyright     Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)  
* @link          http://cakephp.org CakePHP(tm) Project  * @package       app.Model 
 * @since         CakePHP(tm) v 0.2.9  
* @license       http://www.opensource.org/licenses/mit-license.php MIT License 
 */ 
App::uses('Model', 'Model'); 

/**  * Application model for Cake.  *  
* Add your application-wide methods in the class below, your models  
* will inherit them.  *  * @package       app.Model  
*/ 
class User extends Model { //////////////////////////////////////////////////////validation  
var $validate=array( 					
'email'=>array(
						'notempty'=>array(
							'rule'=>array('notEmpty'),
							'message'=>'please enter your email.'
						),
						'email'=>array(
							'rule'=>array('email'),
							'message'=>"Please Enter Valid email."
						
						),
						'uni'=>array(
							'rule'=>array('isUnique'),
							'message'=>'email name already exist.'
						)
						
					),
					'password'=>array(
						'notempty'=>array(
							'rule'=>array('notEmpty'),
							'message'=>'please enter your password.'
						),
						'password'=>array(
							'rule'=>array('minLenght',6),
							'message'=>"please at least six characters."
						
						)
					),
					'f_name'=>array(
						'need'=>array(
							'rule'=>array('notEmpty'),
							'message'=>'This field is required.'
						),
						'lenth'=>array(
							'rule'=>array('minLength',3),
							'message'=>'fname f_name minimum 3 characters.'
						)
					),
					'l_name'=>array(
						'need'=>array(
							'rule'=>array('notEmpty'),
							'message'=>'This field is required.'
						),
						'lenth'=>array(
							'rule'=>array('minLength',3),
							'message'=>'last Name name minimum 3 characters.'
						)
					),
					'mobile'=>array(
						'need'=>array(
							'rule'=>array('notEmpty'),
							'message'=>'This field is required.'
						),
						'lenth'=>array(
							'rule'=>array('minLength',10),
							'message'=>'last Name name minimum 10 characters.'
						),
						'type'=>array(
							'rule' =>array('numeric'),
							'message' => 'Please Enter Only number .'
						)
					),
					

		);



}
validation2



Share Button

About admin

Leave a Reply

Your email address will not be published. Required fields are marked *

*

Author: pramod kumar kumawat Mob: 9269727062
Free WordPress Themes - Download High-quality Templates