Home > Article > Backend Development > ThinkPHP3.1 new features: dynamic setting automatic completion and automatic verification sample code_PHP tutorial
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.