Automatically verifying the instance _ php instance using the ThinkPHP form

Source: Internet
Author: User
This article mainly introduces the ThinkPHP automatic form verification example, which is a very common technique of ThinkPHP, and has very practical value, for more information about how to implement automatic ThinkPHP form verification, see the example in this article. Share it with you for your reference. The specific method is as follows:

The sample code is implemented using the TP 3.2 framework. The code is as follows:

The code is as follows:

Public function add_post (){
// Verification rules
$ Rule = array (
Array ('name', 'require ', 'Enter your name', 1), // name must be verified
);

$ M = M ('user ');

// Obtain name, sex, and contact data to the model and verify
If (! $ M-> field ('name, sex, contact ')-> validate ($ rule)-> create ())
$ This-> error ($ m-> getError ());

$ Result = $ m-> add ();

If (! $ Result)
$ This-> error ('add failed ');

$ This-> success ('added successfully', U ('dir '));
}

The verification rules can also be written into the model, but the author feels a little troublesome. first, sometimes the verification methods on different pages are different, and second, the code in this add_post event is displayed, it is clear what data to receive and how to verify that the data can be roughly understood at the first glance, so this method is summarized.

I hope this article will help you with ThinkPHP program development.

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.