Cakephp自定义数据验证错误消息

edited 十月 2013 in CakePHP
Cakephp提供了强大的自动验证机制,允许我们自定义一些自己的验证消息

我的一个用户注册登陆模块,用到了Auth组件,Auth组件的登陆控制几乎不用我们进行任何控制,我们这里看看如何注册一个用户,并进行用户数据验证

下面记录一个我的自定义数据验证错误的方法.

User控制器代码
function register(){
         //检测用户是否已经登陆
	if($this->Session->check('Auth.User')){
		$this->redirect($this->Auth->loginRedirect);
	}

	if(!empty($this->data)){
		$this->User->set($this->data);
                  //这里验证用户的密码是否匹配
		if($this->data['User']['password'] != $this->Auth->password($this->data['User']['repeatpassword'])){
			$this->User->invalidate('repeatpass');
		}
                  //这里验证用户密码长度
		if(strlen($this->data['User']['repeatpassword']) < 3){
                           //在这里我生成了一个自定义的错误字段,我们稍后在视图中要捕获它
			$this->User->invalidate('password_too_short');
		}
                  //这里转到模型的验证
		if($this->User->validates()){
			$this->User->create();
			if($this->User->save($this->data)){
				$this->Auth->login($this->data);
				$this->redirect($this->Auth->loginRedirect);
			}
		}
		$this->Session->setFlash(__('Sorry ,There are some Errors ,Please retry',true));
	}

         //这里验证没有通过后,要重置用户的密码,因为密码使用了Auth的password方法转义了
	$this->data['User']['password'] = '';
	$this->data['User']['repeatpassword'] = '';
}
User模型代码
class User extends AppModel{
	var $name = 'User';

	var $hasMany = array(
		'ReceivedNote' => array(
			'className' => 'ReceivedNote',
			'foreignKey' => 'receive_user_id'
		),
		'SentNote' => array(
			'className' => 'SentNote',
			'foreignKey' => 'send_user_id'
		),
		'Note'
	);

	var $validate = array(
		'name' => array(
			'alphanumeric' => array(
				'rule' => 'alphaNumeric',
				'message' => 'Alphabets and numbers only'
			),
			'between' => array(
				'rule' => array('between',6,20),
				'message' => 'Between 6 to 20 characters'
			)
		),
		'username' => array(
			'alphanumeric' => array(
				'rule' => 'alphaNumeric',
				'message' => 'Alphabets and numbers only'
			),
			'between' => array(
				'rule' => array('between',6,20),
				'message' => 'Between 6 to 20 characters'
			)
		),
		'password' => array(
			'length' => array(
				'rule' => array('between',40,40),
				'message' => 'Must 40 characters'
			)
		)
	);

	function beforeValidate(){
		if(!$this->id){
			if($this->findCount(array('User.username' => $this->data['User']['username'])) > 0){
                                //这里同样生成了一个自定义错误字段
				$this->invalidate('username_unique');
				return false;
			}
			return true;
		}
	}
}
register视图代码
<?php
echo $form->create('User',array('action' => 'register'));
echo $form->input('User.name');
echo $form->input('User.username',array(
        //这里捕获我们的自定义错误username_unique
	'after' => $form->error(
		'username_unique',
		__('The name has exists ,Please change',true)
	)
));
echo $form->input('User.password',array(
        //这里捕获自定义错误password_too_short
	'after' => $form->error(
		'password_too_short',
		__('Password length must greater than 3 ',true)
	)
));
echo $form->input('User.repeatpassword',array(
	'type' => 'password',
        //同样的这里也捕获了repeatpass自定义错误
	'after' => $form->error(
		'repeatpass',
		__('Repeat password do not match ,Please re-input the Password and RepeatPassword',true)
	)
));
echo $form->end(__('Submit',true));
?>
注意

在捕获自定义错误时,使用$form->error函数可以指定一个或两个参数

第一个参数是自定义错误字段的ID

第二个参数是要显示给用户看的消息,这里我们使用了本地化方法__()函数
引用地址:http://www.thinkly.cn/index.php/archives/185

原始討論: http://twpug.net/x/modules/newbb/viewtopic.php?topic_id=4183
Sign In or Register to comment.