This article mainly introduces ThinkPHP3.1 automatic setting and automatic verification. If you need to set automatic verification or automatic verification before ThinkPHP3.1, generally, attributes must be defined in the model or configured dynamically using the setProperty method. The disadvantage of this method is that it is not convenient for dynamic changes and adjustments.
ThinkPHP3.1 adds two coherent operations: auto and validate in the model class, which are used to dynamically set Automatic completion and automatic verification rules. Now you can use the following operations in the Action:
$ Validate = array ('verify ', 'require', 'verification code is required! '), Array ('name', '', 'account name already exists! ', 0, 'unique', 1),); $ auto = array ('Password', 'md5', 1, 'function '), array ('create _ time', 'time', 2, 'function'),); M ('user')-> auto ($ auto) -> validate ($ validate)-> create ();
The rules for $ auto and $ validate variables are the same as those for the _ auto and _ validate attributes of the model class, and function calls are also supported (due to restrictions of PHP, you cannot call a function in the attribute definition of a class ).
The auto and validate methods must be called before the create method.
With this improvement, you can use the M method to instantiate the model class and then use dynamic settings to complete automatic verification and operations without relying on the D method.