Automatic Verification of the create () method in ThinkPHP, thinkphpcreate

Source: Internet
Author: User

Automatic Verification of the create () method in ThinkPHP, thinkphpcreate

Automatic Verification is a data verification method provided by the ThinkPHP model layer. You can perform automatic data verification when using create to create a data object.

Principle:

The create () method collects the form ($ _ POST) information and returns the information. It also triggers Automatic Form Verification and filters out illegal fields,

When the create () method is used in the Controller (the return value is true/false), the $ _ validate attribute in the Model class is automatically triggered (the method in the parent class Model, in the subclass Model), customize the validation rules in $ _ validate (the verification rules are described in detail below). When the create () method does not have data, that is, the return value is false, get and return error information through the $ xxx object-> getError!

Automatic verification must be defined in the following rule format:

Protected $ _ validate = array (verification field 1, verification rule, error prompt, [verification condition, additional rule, verification time]), array (verification field 2, verification rule, error message: [verification conditions, additional rules, verification time]),...);

The verification field, verification rules, error prompt is required, verification conditions, additional rules, the verification time is optional!

Verification field (required): form field.

Verification rules (required): The require field must be, email address, url address, and number. It can also be used with additional rules.

Error prompt (required): Message returned when verification fails.

Verification condition (optional): There are three options: 0, 1, 2, and field verification in 0: _ POST. The default value is 1. verification is required if the verification rule is defined. 2: verification when the value is not empty.

Additional rules:

Regex Regular Expression verification. The defined validation rule is a regular expression (default)
Function Function verification. The defined verification rule is a function name.
Callback Method verification. The defined verification rule is a method of the current model class.
Confirm Verify that the two fields in the form are the same. The validation rule is defined as a field name.
Equal Whether the verification is equal to a value, which is defined by the previous validation rule
Notequal Verify whether it is not equal to a value. The value is defined by the preceding verification rule (New in version 3.1.2)
In Verify whether the validation rule is within a certain range. It can be an array or a comma-separated string.
Notin Verify whether it is not within a certain range. The defined verification rule can be an array or a comma-separated string (added in version 3.1.2)
Length Verification length. The defined verification rule can be a number (indicating a fixed length) or a number range (for example, 3, 12 indicates a length range from 3 to 12)
Between Verification range. The defined verification rules indicate the range. strings or arrays can be used, for example, 1, 31 or array (1, 31)
Notbetween Verification is not within a certain range. The defined verification rules indicate a range. strings or arrays can be used (added in version 3.1.2)
Expire Whether the verification is in the validity period. The defined verification rules indicate the time range and the time range. For example, 2012-1-15, 2013-1-15 indicate that the current submission is valid between and, you can also use timestamp to define
Ip_allow Verify whether the IP address is allowed. The defined verification rules indicate the list of allowed IP addresses, which are separated by commas (,). For example, 201.12.2.5, 201.12.2.6
Ip_deny Verify whether the IP address is forbidden. The defined verification rules indicate the list of prohibited IP addresses, which are separated by commas (,). For example, 201.12.2.5, 201.12.2.6
Unique The system queries the database based on the current value of the field to determine whether the same value exists. When the form data contains the primary key field, unique cannot be used to determine the primary key field itself.

Verification time (optional): 1, 2, 3; 1: verification when adding data; 2: verification when editing data; 3: Verification in all cases (default ); you can also increase the verification time based on your business needs.

The following code is attached: Take registration as an Example

The front-end page is relatively simple and the code will not be posted. below is the front-end registration interface

Controller code:

// Register public function register () {$ user = new \ Model \ UserModel (); // two Logics: Collection, display if (! Empty ($ _ POST) {// The create () method collects the form ($ _ POST) information and returns it, and triggers automatic form verification, filter illegal fields $ date = $ user-> create (); // use the return value of the create () method $ date to determine whether verification is successful if ($ date) {// Add the array to the string $ date ['user _ hobby'] = implode (',', $ date ['user _ hobby']); $ info = $ user-> add ($ date); if ($ info) {// jump to the home page $ this-> redirect ('index/Index ');}} else {// assign error information to the front-end template $ error = $ user-> getError (); $ this-> assign ('error', $ error );}} // call view $ this-> display ();}

Model Code:

Class UserModel extends Model {// whether to perform batch verification. Obtain all error verification information in batches. protected $ patchValidate = true; // The default value is false. // The Automatic Verification definition is protected $ _ validate = array (// array (field, verification rule, error prompt, verification condition, additional rule, verification time) // ① user name verification, cannot be empty array ('username', 'require ', 'user name cannot be blank'), array ('username ','', 'User name occupied ', '0', 'unique'), // ② password verification, not empty array ('Password', 'require ', 'password cannot be blank '), // ③ verify and confirm the password. It must be filled in. It must be consistent with the password. array ('password2', 'require ', and 'Confirm the password '), array ('password2 ', 'Password', 'two passwords are consistent', 0, 'Confirm'), // ④ email address verification array ('user _ mail ', 'email ', 'mailbox format incorrect', 2), // ⑤ qq verification, number composition, 5-12-Bit array ('user _ qq', 'number ', 'qq must be a number'), array ('user _ qq', '5, 12', 'between 5-12 digits ', 0, 'length '), // 6. You must select an array ('user _ xueli ', '2, 5', 'degree must select a degree', 0, 'between '), // 7. For hobby verification, You must select more than two. // because the hobby returns an array and no rules can be directly used in the additional rules, you need to customize the method, use the callback method to verify array ('user _ hobby', 'check _ hobby', 'two or more hobbies are required ', 1, 'callback '),); // define the method for hobby verification // The parameter $ arg represents the verified form information function check_holobby ($ arg) {// determine whether the array length is greater than 2 if (count ($ arg) <2) {return false; // The verification error message will be automatically output} return true ;}}

Display the verification error information in the template (partial code)

<Td> for = "User_username" class = "required"> User Name <span> * </span> </label>
</Td> <td> class = "inputBg" size = "25" name = "username" id = "User_username" type = "text" value = ""/> <span> <{$ error. username | default: "" }></span> </td>

Result:

 

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.