ThinkPHP form automatic verification function, thinkphp form verification _ PHP Tutorial

Source: Internet
Author: User
The automatic form verification function of thinkPHP and thinkphp form verification. ThinkPHP form automatic verification function. thinkphp form verification last night, our boss told me to get the form automatic verification function. I learned a lot from the official website for a long time before I came out, total thinkPHP form automatic verification function, thinkphp form verification

Last night, our boss told me to get the form automatic verification function. I had to learn a lot from the official website for a long time before coming out. Hey, let's share my own achievements!

Thinkphp defines automatically verified functions and regular expressions for us in the Model base class. we only need to create the $ _ validate attribute under the Model class of the corresponding database table.

1. find the Model base class and you will see protected $ _ validate = array (); // automatically verify the setting. it is of the array type and is defined in the corresponding data Model file below;

2,

public function CheckVerify($verify) {   if (md5($verify) != Session::get('verify')) return false;        return true;}

// Automatic verification

Protected $ _ validate = array ("title", "require", "the title is required! "), Array ('categoryid', 'require '," category is required! "), Array ('content', 'require '," the content is required! "), Array ('verify ', 'require', 'verification code is required! '), Array ('verify', 'checkverify ', 'verification code error! ', 0, 'callback '));


3. Format Description: array (verification field, verification rule, error prompt, verification condition, additional rule, verification time ),

4. parameter explanation:

Verification field: the name of the form field to be verified. This field is not necessarily a database field, but also an auxiliary field of the form, such as confirming the password and verification code.
Verification rules: the rules to be verified must be combined with the additional rules (required). The officially included rules are as follows (you can also add them yourself ):

 1 $validate = array( 2  3             'require'=> '/.+/', 4  5             'email' => '/^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/', 6  7             'url' => '/^http:\/\/[A-Za-z0-9]+\.[A-Za-z0-9]+[\/=\?%\-&_~`@[\]\':+!]*([^<>\"\"])*$/', 8  9             'currency' => '/^\d+(\.\d+)?$/',10 11             'number' => '/^\d+$/',12 13             'zip' => '/^[1-9]\d{5}$/',14 15             'integer' => '/^[-\+]?\d+$/',16 17             'double' => '/^[-\+]?\d+(\.\d+)?$/',18 19             'english' => '/^[A-Za-z]+$/',20 21         );

Prompt information: defines the prompt information (required) and ),
Verification criteria: (optional)
There are three verification criteria:
Model: EXISTS_TO_VAILIDATE or 0 (default)
Model: MUST_TO_VALIDATE or 1 must be verified
Verification when Model: VALUE_TO_VAILIDATE or 2 is not null
Additional rules:
Regex regular expression verification indicates that the previous validation rule is a regular expression;
Function verification indicates that the previous verification is a function name;
Callback uses method verification, indicating that the verification rule is a method of the Model class;
Confirm verifies that the two fields in the form are equal, and the verification rule is a field name;
Equal verification is equal to a value, which is defined by the preceding verification rules;
In to verify whether it is within a certain range. the preceding definition is an array;
Unique verifies the uniqueness. The system queries the database based on the current value of the field to determine whether the same value exists;
At the same time, the system also has built-in some common regular expression verification rules, which can be used in this section, including: required require field, email;
Currency, number. These verification rules can be used directly;

Verification Time: (optional)

01. Model: MODEL_INSERT or 1 verification when adding data
02. Model: MODEL_UPDATE or 2 verification when editing data
03. Model: MODEL_BOTH or 3 verification in all cases (default)

5. official example:

Protected $ _ validate = array ('verify ', 'require', 'verification code is required! '), // Verify array (name, '', 'account name already exists with regular expressions by default! ', 0, 'Unique', 1), // verify that the name field is unique when it is added. array (1, 2, 3 ), 'value range is incorrect! ', 2, 'in'), // when the value is not empty, determine whether it is in a range array ('repassword', 'password', 'confirm the password is incorrect ', 0, 'confirm'), // verify that the password is consistent with the password array ('password', 'checkpwd', 'incorrect password format', 0, 'Function ') // customize the password format for function verification );

I don't know if it meets the requirements of our boss. In short, it's still coming out. it's so hard !! Find the code and case in the evening, and find comfort !!

Http://www.bkjia.com/PHPjc/930046.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/930046.htmlTechArticlethinkPHP form automatic verification function, thinkphp form verification last night we boss told me to get form automatic verification function, worry for a long time to learn a lot of knowledge of the official website, just come out, ah, total...

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.