Thinkphp automatic verification-php Tutorial

Source: Internet
Author: User
Thinkphp automatic verification problem where is the function verification required for automatic verification during thinkphp registration? Where can I call a completed verification function? ------ Solution ------------------ the official documentation clearly states that the ThinkPHP5.3.15 automatic verification type check is only for database-level verification. Therefore, the system also has the built-in automatic verification function for data objects to complete model business rule verification, in most cases, thinkphp automatically verifies the problem.
Where is the function verification required for automatic verification during thinkphp registration?
Where can I call a completed verification function?

------ Solution --------------------
The official documentation is clear:


ThinkPHP

5.3.15 automatic verification
The type check is only for database-level verification. Therefore, the system also has the built-in automatic verification function for data objects to verify the business rules of the model. In most cases, the data object is created by the $ _ POST data submitted by the form. To use the automatic verification function of the system, you only need to define the $ _ validate attribute in the Model class, which is an array composed of multiple verification factors. the supported verification factor format is:


PHP code


Array (verification field, verification rule, error prompt, verification condition, additional rule, verification time)



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. (Required)

Verification rules: Rules to be verified must be combined with additional rules (required)

Prompt information: defines the prompt information after verification fails (required)

Verification criteria: (optional)


PHP code


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: used with verification rules (optional), including:

? Regex uses regular expressions for verification, indicating that the verification rule defined previously is a regular expression (default)

? Function verification is used. the verification rule defined previously is a function name.

? Callback uses method verification. the verification rule defined earlier is a method of the current Model class.

? Confirm verifies whether the two fields in the form are the same. the verification rule defined above is a field name.

? Equal verification is equal to a value, which is defined by the preceding validation rules

? In verification is within a certain range. The Validation rule defined earlier must be an array

? Unique verification is unique. The system queries the database based on the current value of the field to determine whether the same value exists.

The system also has built-in rules for common regular expression verification, which can be used directly, including: required require field, email address, url URL address, currency, number, these verification rules can be used directly.

Verification Time: (optional)


PHP code


Model: MODEL_INSERT or 1 verification when adding data

Model: MODEL_UPDATE or 2 verification when editing data

Model: MODEL_BOTH or 3 verification in all cases (default)



Example:


PHP code


Protected $ _ validate = array (

Array ('verify ', 'require', 'verification code is required! '), // Use regular expressions for verification by default

Array (name, '', 'account name already exists! ', 0, 'Unique', 1), // verify that the name field is unique when it is added.

Array ('value', array (, 3), 'value range is incorrect! ', 2, 'in'), // determines whether the value is within the specified range when the value is not empty.

Array ('repassword', 'password', 'confirm password is incorrect ', 0, 'confirm'), // verify that the password is consistent with the password

Array ('password', 'checkpwd', 'incorrect password format', 0, 'Function'), // customize function verification password format

);



When you create a data object using the create method of the system, data verification is automatically performed. sample code:


PHP code


$ User = D ("User"); // instantiate the User object

If (! $ User-> create ()){

// If creation fails, the verification fails and an error message is displayed.

Exit ($ User-> getError ());

} Else {

// Other data operations can be performed after verification

}



Generally, the verification rules for each data table are relatively fixed, but in some special cases, the verification rules may be changed, we can dynamically change the verification rules to meet the verification under different conditions:


PHP code


$ User = D ("User"); // instantiate the User object

$ Validate = array (

Array ('verify ', 'require', 'verification code is required! '), // You only need to verify the verification code.

);

$ User-> setProperty ("_ validate", $ validate );

$ Result = $ User-> create ();

If (! $ Result ){

// If creation fails, the verification fails and an error message is displayed.

Exit ($ User-> getError ());

} Else {

// Other data operations can be performed after verification

}

------ Solution --------------------
Discussion
I do not know where the checkPwd function is,
Array ('password', 'checkpwd', 'incorrect password format', 0, 'Function'), // customize function verification password format

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.