This article mainly introduces the ThinkPHP form automatic verification example. it is a very common technique of ThinkPHP three major automatic verification and is very useful. if you need ThinkPHP, you can refer to ThinkPHP.
This article describes how to implement automatic verification of ThinkPHP forms. 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.