在CakePHP中,对于模型数据的验证是非常简单的,下面就是我在Blog Model中用到的验证方法:
/**
* Blog validate rule
*
* @var array
* @access public
*/
var $validate = array(
'title'=>array(
'notEmpty' => array(
'rule' => VALID_NOT_EMPTY,
'message' =>'The title is not empty'
),
'notBlank' => array(
'rule' => '/[^\\s]/',
'message' => 'Please input the title!'
)
),
'description'=>array(
'notEmpty' => array(
'rule' => VALID_NOT_EMPTY,
'message' =>'The blog\'s content is not empty'
),
'notBlank' => array(
'rule' => '/[^\\s]/',
'message' => 'Please input the content!'
)
)
);
当然,这是必须的.虽然,客户端验证看起来更加友好,但为了最大限度的防止数据有效性,server的数据验证显的尤为重要!
下面我将全面的绍介下,CakePHP中对于数据验证的规则设定和内置函数以及处理方法:
验证规则
1. Simple Rules (普通的)
var $varlidate = array('fieldName' => 'ruleName');
2. One Rule Per Field (单的)
var $varlidate = array(
'fieldName1' => array(
'rule' => 'ruleName', //or: array('ruleName', 'param1', 'param2' ...)
'required' => true,
'allowEmpty' => false,
'on' => 'create', //or: 'update'
'message' => 'Error Message'
)
);
3. Multiple Rules per Field (多个)
var $validate = array(
'fieldName' => array(
'ruleName' => array(
'rule' => 'ruleName',
),
'ruleName2' => array(
'rule' => 'ruleName2',
)
)
);
CakePHP在frame内部内置了很多(详见cake/libs/validation.php中)非常常见的函数来实现了数据验证:
一、内置验证规则
1. alphaNumeric
只包含字母和数字:
var $validate = array(
'login' => array(
'rule' => 'alphaNumeric',
'message' => 'Usernames must only contain letters and numbers.'
)
);
2. between
数据长度必须属于一个指定范围,必须提供最小值和最大值:
var $validate = array(
'password' => array(
'rule' => array('between', 5, 15),
'message' => 'Passwords must be between 5 and 15 characters long.'
)
);
3. blank
字符是否为空,或仅包含空白字符,例如space,tab :
var $validate = array(
'id' => array(
'rule' => 'blank',
'on' => 'create'
)
);
4. cc
验证信用卡号码是否有效。它需要3个参数:'type', 'deep' and 'regex':
var $validate = array(
'ccnumber' => array(
'rule' => array('cc', array('visa', 'maestro'), false, null),
'message' => 'The credit card number you supplied was invalid.'
)
);
5. comparison
用来比较数值:
var $validate = array(
'age' => array(
'rule' => array('comparison', '>=', 18),
'message' => 'Must be at least 18 years old to qualify.'
)
);
var $validate = array(
'age' => array(
'rule' => array('comparison', 'greater or equal', 18),
'message' => 'Must be at least 18 years old to qualify.'
)
);
6. date
确保数据提交有效的日期格式:
var $validate = array(
'born' => array(
'rule' => 'date',
'message' => 'Enter a valid date in YY-MM-DD format.',
'allowEmpty' => true
)
);
7. decimal
用来检查提交的数据是否是一个有效的十进制整数:
var $validate = array(
'price' => array(
'rule' => array('decimal', 2)
)
);
8. email
用来检查输入是否是一个有效的 email 地址(非常常用):
var $validate = array('email' => array('rule' => 'email'));
var $validate = array(
'email' => array(
'rule' => array('email', true),
'message' => 'Please supply a valid email address.'
)
);
9. equalTo
将确保该值等于,和同类型的特定价值:
var $validate = array(
'food' => array(
'rule' => array('equalTo', 'cake'),
'message' => 'This value must be the string cake'
)
);
10. extension
检查文件的扩展名是否为指定格式,一般用array来限定扩展名:
var $validate = array(
'image' => array(
'rule' => array('extension', array('gif', 'jpeg', 'png', 'jpg'),
'message' => 'Please supply a valid image.'
)
);
11. ip
确保提交的是有效的IPv4地址:
var $validate = array(
'clientip' => array(
'rule' => 'ip',
'message' => 'Please supply a valid IP address.'
)
);
12. isUnique
确保该字段是唯一的:
var $validate = array(
'name' => array(
'rule' => 'isUnique',
'message' => 'This name has already been taken.'
)
);
13. minLength
确保数据符合最小长度要求:
var $validate = array(
'password' => array(
'rule' => array('minLength', '8'),
'message' => 'Password must be at least 8 characters long.'
)
);
14. maxLength
确保数据符合最大长度要求:
var $validate = array(
'name' => array(
'rule' => array('maxLength', '15'),
'message' => 'Names must be no larger than 15 characters long.'
)
);
15. money
确保该值是一个有效的货币:第二个参数定义符号位于(left/right):
var $validate = array(
'salary' => array(
'rule' => array('money', 'left'),
'message' => 'Please supply a valid monetary amount.'
)
);
16. inList
这条规则将确保该值属于某一(通常是array)集合:
var $validate = array(
'function' => array(
'allowedChoice' => array(
'rule' => array('inList', array('Foo', 'Bar')),
'message' => 'Enter either Foo or Bar.'
)
)
);
17. numeric
是否是一个有效的号码(数字):
var $validate = array(
'carNum' => array(
'rule' => 'numeric',
'message' => 'Please supply the number of cars.'
)
);
18. notEmpty
确保字段不为空(常用):
var $validate = array(
'title' => array(
'rule' => 'notEmpty',
'message' => 'This field cannot be left blank'
)
);
19. phone
验证一个合法的电话号码,缺省为'all'.
如果你想验证某个国家的电话号码,你可以提供一个表达的第二个参数,用以支持其它格式:
var $validate = array(
'phone' => array(
'rule' => array('phone', null, 'us')
)
);
20. postal
用来验证邮政编码,缺省为'null'.
可以是美国(us),加拿大(ca) ,英国(uk),德国( de )和比利时(be).
对于其他邮递区号格式,您可以提供一个正则表达式作为第二个参数;
var $validate = array(
'zipcode' => array(
'rule' => array('postal', null, 'us')
)
);
21. range
保证值是在特定的范围内:
var $validate = array(
'number' => array(
'rule' => array('range', 0, 10),
'message' => 'Please enter a number between 0 and 10'
)
);
22. ssn
ssn(美国)有效的社会安全号码,可以是美国(us),丹麦(dk),荷兰(nl) .
23. url
检查有效的URL格式。
支持HTTP(s)的FTP(s),file,news和的Gopher协议:
var $validate = array(
'urls' => array(
'rule' => 'url'
)
);
二、Custom Validation Rules (自定义验证)
1. Custom Regular Expression Validation (自定义正则表达式验证)
var $validate = array(
'username' => array(
'rule' => array('custom', '/[a-z0-9]{4,}$/i'),
'message' => 'Only letters and integers, min 4 characters'
)
);
以上,检查的username只包含字母和整数,并且不少于四个字符.
2. Custom Validataion Methods (自定义方法验证)
<?php
class User extends AppModel {
var $name = 'User';
var $validate = array(
'code' => array(
'rule' => array('limitDuplicates', 25),
'message' => 'This code has been used too many times.'
)
);
function limitDuplicates($data, $limit){
$existing_count = $this->find( 'count', array('conditions' => $data, 'recursive' => -1) );
return $existing_count < $limit;
}
}
?>
这个自定义方法避免了code在数据库中出现的次数不超过一定的次数(25).
以上就是CakePHP中关于server端数据验证的方法汇总,如有缈误,欢迎指出,欢迎留言评论!