ThinkPHP form automatic submission verification instance tutorial _ php instance

Source: Internet
Author: User
This article mainly introduces the ThinkPHP form automatic submission and verification example tutorial, which is very useful. If you need it, you can refer to this article to illustrate the implementation process of ThinkPHP form automatic submission and verification, the detailed steps are as follows:

1. template:

 ThinkPHP example: Form submission, automatic verification, and Automatic Filling

Ii. Model part form program code:

<? Php class FormModel extends Model {// Automatic Verification setting protected $ _ validate = array (// This must be defined as $ _ validata to verify array ('title', 'require ', 'The title is required! ', 1), array ('email', 'email ', 'email format error! ', 2), array ('content', 'require', 'content required '), array ('verify', 'require ', 'verification Code required! '), Array ('verify', 'checkverify ', 'verification code error', 0, 'callback'), // call back method verification, the verification rule defined above is a method of the current Model class // if you do not understand it here, you can refer to the last method of this class array ('title ','', 'title already exists ', 0, 'unique', 'add'), // additional verification unique, unique verification is unique, the system will query the database based on the current value of the field to determine whether the same value exists);/* think's verification mechanism saves a lot of trouble: equire email: the email verification code is correct: checkVerify array () array content array (verification field, verification rule, error prompt, verification condition, additional rules, verification time) Verification condition EXISTS_TO_VAILIDATE or 0 field exists for verification (default) MUST_TO_VALIDATE or 1 must be verified When the VALUE_TO_VAILIDATE or 2 value is not blank, verify the append rule in combination with the verification rule. For example, function verification is used. The verification rule defined above is the method used for function name callback verification, the verification rule defined earlier is the confirm method of the current Model class to verify whether the two fields in the form are the same. The verification rule defined earlier is a field name equal to verify whether it is equal to a value, this value is defined by the preceding validation rule in to verify whether it is within a certain range. The previously defined validation rule must be an array unique to verify whether it is unique, the system queries the database based on the current value of the field to determine whether the same value regex exists for verification using regular expressions. This indicates that the verification rule previously defined is a regular expression (default) * /// auto-fill setting // derived from the above, the following is an auto-fill field, it is convenient to understand protected $ _ auto = array (// it must also be defined as $ _ auto array ('statu S ', '1', 'add'), array ('create _ time', 'time', 'add', 'function '), // specify the filling function time () here./* array factor: array (fill field, fill content, fill condition, additional rules) Fill conditions include: when adding new data (the default method), when updating data, when processing ALL data, the additional rules include: function uses the callback Method field to fill the string with other fields (default) */public function CheckVerify () {return md5 ($ _ POST ['verify ']) ==$ _ SESSION ['verify '] ;}}?>

3. The indexaction program code of the controller is as follows:

<? Php class IndexAction extends Action {// home page public function index () {$ Form = D ("Form "); // create an object $ list = $ Form-> top6 ('', '*', 'Id desc '); // read the latest 6 records from the database, and the security id is output in reverse order $ this-> assign ('LIST', $ list); // transmits the data to the template $ this-> display ();} // process form data public function insert () {// ACTION of the form corresponding to this method = "/index. php/Article/insert "$ Form = D (" Form "); if ($ Form-> create () {// create Form data object, by default, the data submitted through the Form is created to prepare for writing the following data to the database $ Form-> add (); // add For the data submitted in the form, submit the data object created above $ this-> redirect (); // return the previous module, page Jump can be said to be} else {header ("Content-Type: text/html; charset = UTF-8"); exit ($ Form-> getError (). '[back to]');} // generate the verification code // This method has nothing to say, fixed format: public function verify () {import ("ORG. util. image "); Image: buildImageVerify (); // here the two colons call static methods }}?>

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.