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.