Automatic Verification of thinkphp version 3.1

Source: Internet
Author: User
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:

  1. $ User = D ('user ');
  2. $ Data ['username'] =_ _ POST ['username'];
  3. $ Data ['password'] = $ _ POST ['password'];
  4. $ Data ['email '] = $ _ POST ['email'];
  5. $ Validate = array (
  6. Array ('username', 'require ', 'user name cannot be blank', 1 ),
  7. Array ('username', '', 'user name already exists ', 1, 'Unique', 1 ),
  8. Array ('confirm _ password', 'password', 'confirm incorrect password', 0, 'confirm '),
  9. );
  10. $ Auto = array (
  11. Array ('password', 'md5', 1, 'Function '),
  12. Array ('Reg _ time', 'time', 1, 'Function '),
  13. );
  14. If ($ user-> auto ($ auto)-> validate ($ validate)-> create ())
  15. {
  16.  
  17. $ User-> add ();
  18. Echo $ user-> getLastSql (); exit;
  19. }
  20. Else
  21. {
  22. $ This-> error ($ user-> getError ());
  23. }

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:

  1. $ User = D ('user ');
  2. If ($ user-> getByUsername ($ _ POST ['username'])
  3. {
  4. // Info
  5. // $ This-> success ('username already exists ', '123 ');
  6. $ This-> ajaxReturn (array ('info' => 'cunzai', 'SS' => 'ddddd'), "already exists! ", 1 );
  7. // The success method is different from the ajaxReturn method. You can return custom settings in ajaxReturn. Fixed response parameters in success Mode
  8.  
  9. }
  10. Else
  11. {
  12. $ This-> error ('user name does not exist ');
  13.  
  14. }

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.