Home  >  Article  >  Backend Development  >  ThinkPHP3.1 new features: dynamic setting automatic completion and automatic verification sample code_PHP tutorial

ThinkPHP3.1 new features: dynamic setting automatic completion and automatic verification sample code_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 10:24:14751browse

Before ThinkPHP 3.1, if you need to set up automatic verification or automatic completion, you must define it in the model, or dynamically set properties through the setProperty method. However, the disadvantage of this is that it is not convenient for dynamic changes and adjustments.

ThinkPHP3.1 version adds two coherent operations, auto and validate, to the model class, which are used to dynamically set auto-complete and auto-validation rules. They can now be used in Action. The sample code is as follows:

$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();

The specifications of $auto and $validate variables are consistent with the definition rules of the _auto and _validate attributes of the model class, and they can also support function calls (due to limitations of PHP itself, functions cannot be called in the attribute definition of the class) .

The auto and validate methods must be called before the create method.

Through this improvement, you can instantiate the model class through the M method and then use dynamic settings to complete automatic verification and automatic completion operations. You no longer have to rely on the D method.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/825522.htmlTechArticleBefore the ThinkPHP3.1 version, if you need to set up automatic verification or automatic completion, you must define it in the model, or pass The setProperty method dynamically sets properties to complete, but to do so...
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