Thinkphp Framework Project Development topic-Automatic verification
In the development of Web projects, any module of the project will have the function of data adding--such as user registration, news addition, commodity management, etc., then we will use the form to transfer data, and when the data is added, neither the foreground user nor the background administrator can avoid filling invalid data or error data. Then the validation of the form data is essential to our project. So if you participate in or independently develop the Web project, it will be found in the addition of data when a large number of data validation-such as the existence of the user name, verification code is correct, the password is consistent, whether the age is a number, etc., then will use a large number of database lookup, regular expression of the writing and other problems to haunt everyone.
In thinkphp, the automatic validation and auto-completion of data objects are built into the business rules validation of the model, and in most cases the data objects are created by the $_post data submitted by the form. You need to use the system's automatic validation function, just define the $_validate property within the model class.
Validation factor format for the $_validate property:
Array (validation fields, validation rules, error prompts, validation criteria, additional rules, validation time)
* Validation field: Form field name, note: This is not necessarily a database field, it can also be a form of some auxiliary fields-such as confirmation password and verification code and so on.
* Validation rules: Rules to be validated, sometimes with additional rules.
Built-in validation rules-including: Require field must, email mailbox, URL URL address, currency currency, number numbers, these validation rules can be used directly. Note: If you have other validation rules, you need to write your own method
* Hint information: Used to verify the failure of the message.
Validation Criteria :
0 or model::exists_to_vailidate--exists field is verified (default)
1 or model::must_to_validate--must verify
2 or the model::value_to_vailidate--value is not empty when validating
Additional rules: Working with validation rules
The Regex uses regular validation to indicate that the validation rule defined earlier is a regular expression (default)
function validation is used by functions, and the validation rule defined above is a function name Note: System functions or Custom functions
Callback using method validation, the previously defined validation rule is a method of the current model class Note: custom methods
Confirm verify that the two fields in the form are the same, the validation rule defined above is a field name
Equal verifies that the value is equal to a value that is defined by the preceding validation rule
In verifies whether the note is in a range: The validation rule defined earlier must be an array
Unique authentication is only, the system will query the database based on the current value of the field to determine whether the same value note: The database is requested
Validation Time :
1 or Model:: model_insert--validation when new data is added
2 or Model:: model_update--edit data when validating
3 or Model:: model_both--All cases verified (default)
Once you are familiar with the $_validate attribute, use examples to analyze how to write validation factors when validating certain data.
If the students who have studied thinkphp should have seen some examples of automatic verification in the manual, here we will summarize most of the common examples here, convenient for everyone to learn and use, if there are some students commonly used but there is no mention here that everyone can brainstorm, To improve all of the methods of validating data automatically, this will be updated constantly.
Instance:
Protected $_validate = Array (
Array (' username ', ' require ', ' username must! '),///data is empty NOTE: The default increase is verified by the modification
Array (' username ', ' ', ' username already exists! ', 0, ' unique ', 1),//Verify that the Username field is unique when added
Array (' Password ', ' checkpwd ', ' Password format incorrect ', 0, ' function '),//password format can be customized with Chenkpwd method
Array (' Repassword ', ' Password ', ' Confirm password incorrect ', 0, ' confirm '),//Verify that the password is consistent with the password
Array (' Sex ', ' array (0,1,2) ', ' Gender must be 0,1,2 ', 0, ' in '),//Verify that the data is within a range
Array (' age ', ' number ', ' ages must be numbers '),//Verify data is numeric
Array (' email ', ' email ', ' mailbox format '),//built-in regular verification mailbox
Array (' email ', '/^/w+ ' ([-+.] /w+) *@/w+ ([-.] /w+) */./w+ ([-.] /w+) *$/', ' mailbox format is incorrect,//custom regular validation data
Array (' MyPage ', ' url ', ' Personal URL format incorrect '),//built-in regular authentication URL address
Array (' Verify ', ' * * * ', ' captcha incorrect ', 0, ' equal '),//Verify that the data equals a certain value NOTE: * * * can be a random code
Array (' salary ', ' currency ', ' salary verification incorrect ', ' 0 '),//built-in verification currency data
);
Thinkphp's automatic verification function can meet virtually any data validation requirement, so when you add and validate data in a project with thinkphp, you don't have to worry about validating the data with automatic validation and auto-completion. Automatic completion of the next explanation and summary of the function, and will be released thinkphp Project module production and project examples, I hope you support.