After the new version is released, you can also verify it in the controller. you need to specify two variables: the rules specified in $ validate $ auto are the same as those in the model. There is always a problem when we put automatic verification in the model. we need to instantiate the model class D first.
After the new version is released, you can also verify it in the controller. you need to specify two variables: the rules specified in $ validate $ auto are the same as those in the model.
There is always a problem when we put automatic verification in the model. we need to instantiate the model class D first.
Note:Automatic verification is supported only when $ user-> auto ($ auto)-> validate ($ validate)-> create () generates an object.
Then, the date array should be retained, because the registration like I did would be extracted from the post array without date.
User-> add (); is unsuccessful. by default, it is post data. with the date array, you should add the date array and put it in the controller. everything is normal. Code:
- $ User = D ('user ');
- $ Data ['username'] =_ _ POST ['username'];
- $ Data ['password'] = $ _ POST ['password'];
- $ Data ['email '] = $ _ POST ['email'];
- $ Validate = array (
- Array ('username', 'require ', 'user name cannot be blank', 1 ),
- Array ('username', '', 'user name already exists ', 1, 'Unique', 1 ),
- Array ('confirm _ password', 'password', 'confirm incorrect password', 0, 'confirm '),
- );
- $ Auto = array (
- Array ('password', 'md5', 1, 'Function '),
- Array ('Reg _ time', 'time', 1, 'Function '),
- );
- If ($ user-> auto ($ auto)-> validate ($ validate)-> create ())
- {
-
- $ User-> add ();
- Echo $ user-> getLastSql (); exit;
- }
- Else
- {
- $ This-> error ($ user-> getError ());
- }
Ajax verification,In thinkphp, the $. post method verifies whether the user name exists or does not exist. the instance code is as follows:
$. Post ('_ URL _/checks', {'username': s, 'A': 'BB'}, function (data ){
Alert (data.data.info );
});
The php code is as follows:
- $ User = D ('user ');
- If ($ user-> getByUsername ($ _ POST ['username'])
- {
- // Info
- // $ This-> success ('username already exists ', '123 ');
- $ This-> ajaxReturn (array ('info' => 'cunzai', 'SS' => 'ddddd'), "already exists! ", 1 );
- // The success method is different from the ajaxReturn method. You can return custom settings in ajaxReturn. Fixed response parameters in success Mode
-
- }
- Else
- {
- $ This-> error ('user name does not exist ');
-
- }