Thinkphp5 model validator execution sequence

Source: Internet
Author: User
Tags add time

Thinkphp5 model validator execution sequence

Thinkphp5 classifies the model verification rules as a validators. This practice does not conform to the expectations of everyone, but the landlord is quite uncomfortable.

The landlord prefers to write the verification rules of tp3.2 directly in the model. After all, your verification rules are generally used for model verification. Independent,

I personally feel that apart from writing one more file, the advantage is temporarily unavailable.

When writing the login module, I suddenly thought that the automatic completion rules can be defined in the model, and the validators can define verification rules.

So what is the sequential execution relationship between the two? Manually verify:

Ideas:

First fill in the data in the controller, and then try to add records in the database. Verify the information of the Logon account of the data:

Verification 1: The accounts field is not entered in the Controller. In the model, the accounts field is filled in automatically by completing rules. Then, output the content of the accounts field in the validator's function.

Verification 2: Enter the accounts field in the controller. Enter the accounts field content automatically in the model (inconsistent with the Controller)

Login table design:

1 drop table if exists 'tp5 _ user'; 2 /*! 40101 SET @ saved_cs_client = @ character_set_client */; 3 /*! 40101 SET character_set_client = utf8 */; 4 create table 'tp5 _ user' (5 'id' int (16) unsigned not null AUTO_INCREMENT COMMENT 'id ', 6 'username' varchar (16) not null comment 'username', 7 'accounts' varchar (255) not null comment' login account', 8 'Password' varchar (255) not null comment 'login password', 9 'mobile' varchar (15) default null comment' (Reset Password) ', 10 'user _ type' tinyint (4) DEFAULT '0' comment' Account type ', 11 'status' tinyint (4) DEFAULT '1' comment' status (0 disabled, 1 normal )', 12 'create _ time' datetime default null comment' add time', 13 'Update _ time' datetime default null comment 'Update time', 14 primary key ('id') 15) ENGINE = MyISAM AUTO_INCREMENT = 2 default charset = utf8 COMMENT = 'customer table ';

Model Definition:

1 namespace app\index\model;2 3 use think\Model;4 5 class User extends Model {6     protected $auto = ['accounts' => 'gxk'];7 }

 

Validator definition:

1 namespace app \ index \ validate; 2 3 use think \ Validate; 4 5 // test checker 6 class User extends Validate {7 protected $ rule = [8 'username' => 'require | max: 25 | check_name ', 9 'accounts' => 'check _ accounts', 10 // 'email '=> 'email', 11]; 12 13 protected $ message = [14 'username. require '=> 'name required', 15' name. max '=>' name cannot exceed 25 characters ', 16 // 'email. check_email '=> 'mailbox question', 17 // 'email' => 'mailbox format error', 18]; 19 20/*** check 22 * fixed parameters of the custom verification function ~~~ 23 * @ param $ value the field information passed in 24 * @ param $ rule the parameter information passed in 25 * @ param $ data all parameter information submitted 26 */27 protected function check_email ($ value, $ rule, $ data) {28 dump ($ value); 29 dump ($ data); 30 return false; 31} 32 33 protected function check_name ($ value, $ rule, $ data) {34 dump ($ data); 35 dump ($ value); 36 return true; 37} 38 39 protected function check_accounts ($ value, $ rule, $ data) {40 dump ($ value); 41 return true; 42} 43}

Controller:

1 namespace app \ index \ controller; 2 3 class User {4 public function index () {5 $ user = new \ app \ index \ model \ User (); 6 $ data = ['username' => 'gxk', 'mobile' => '000000', 'accounts' => 'aaa']; 7 // call the User validator class corresponding to the current model for data verification 8 $ result = $ user-> validate (true)-> save ($ data ); 9 if (false ===$ result) {10 // error message 11 dump ($ user-> getError (); 12} 13} 14}

Verify j results:

Verification 1:

If the Controller does not enter the accounts field and the validator defines the check_accounts function for checking the accounts data, you will find that this function has not been executed, but the data record has been added.

The value of the accounts field is the automatically filled value of the model. Note: The model is automatically filled after the validators. If the require rule is not defined when a field validation rule is defined in the rule,

If this field is not in the submitted data, the verification rule is not executed (the verification function ).

Verification 2:

The Controller fills in the accounts field, and then the model automatically fills in inconsistent data according to the rules. It finds that the verification function is executed and the database writes data. It is found that the value of the field changed in the database is the value of the controller. Note that if

If the data submitted by the controller contains the field value, the value of this field is included in the Automatic completion field of the model, but it is overwritten.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.