Home  >  Article  >  Backend Development  >  ThinkPHP3.1新特性之动态设置自动完成和自动验证示例_php实例

ThinkPHP3.1新特性之动态设置自动完成和自动验证示例_php实例

WBOY
WBOYOriginal
2016-06-07 17:18:40793browse

以往在ThinkPHP3.1版本之前,如果需要设置自动验证或者自动完成,一般来说必须定义在模型中,或者通过setProperty方法动态设置属性来完成,这样做的缺点是不太方便动态改变和调整。

ThinkPHP3.1版本在模型类中增加auto和validate两个连贯操作,用于动态设置自动完成和自动验证规则,现在可以在Action中使用:

$validate = array(
  array('verify','require','验证码必须!'), 
  array('name','','帐号名称已经存在!',0,'unique',1), 
 );
$auto = array ( 
  array('password','md5',1,'function') , 
  array('create_time','time',2,'function'), 
 );
M('User')->auto($auto)->validate($validate)->create();

其中$auto和$validate变量的规范和模型类的_auto和_validate属性的定义规则一致,而且还可以支持函数调用(由于PHP本身的限制,在类的属性定义中不能调用函数)。

auto和validate方法必须在create方法之前被调用。

通过这一改进,你完全可以通过M方法实例化模型类后使用动态设置完成自动验证和自动完成操作,不必再依赖D方法了。

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn