Thinkphp has built-in data object's automatic verification function to complete the model's business rule verification. Automatic validation is based on data objects, and most of the data objects are created based on $_post forms (not absolute).
The basic automatic verification features include: Must fields, email mailbox format, uniqueness, URL address, number, two fields are the same, whether equal to a value, and so on.
To use the system's automatic verification function, you only need to define the $_validate attribute (an array of multiple validation factors) in the corresponding model class, and the syntax is as follows:
Protected $_validate = Array (
validation fields, validation rules, error prompts [, validation conditions] [, additional rules] [, Verification time])
};
thinkphp Automatic validation format description:
| validation Factor |
Description |
| Validating fields |
Have to. The name of the form field that needs to be validated, which is not necessarily the database fields or some of the secondary fields of the form, such as validation code. |
| Validation rules |
Have to. The rules for validation need to be used in conjunction with additional rules. |
| Error tips |
Have to. When validation does not pass the outdated hint information. |
| Validation criteria |
Optional. Includes: Model::exists_to_vailidate or 0: Validation of fields on (default) Model::must_to_validate or 1: Must verify Model::value_to_vailidate or 2: When the value is not empty Waiting for verification |
| Additional Rules |
Optional. Used with validation rules, including: Regex: Use regular to verify that the preceding validation rule is a regular expression (default) Unique: Validation uniqueness, the system queries the database based on the current value of the field to determine if the same value exists Confirm: Verify that the two fields in the form are the same and that the preceding validation rule is a field name equal: Verify that the value is equal to a value that is defined by the preceding validation rule in: Verify that the validation rule defined earlier must be an array Functions: Use function validation to indicate that the validation rule defined earlier is a function name callback: Using method validation, the validation rule defined earlier is a method of the current Model class |
| Verification time |
Optional. Includes: Model:: Model_insert or 1: New data validation Model:: Model_update or 2: Edit the data when validation model:: Model_both or 3: Verify all cases (default) |
Automatic Validation Example
The following example verifies that the title of the article must be filled out and that the test title exists:
Class Articlemodel extends model{
protected $_validate = Array (
' title ', ' Require ', ' title cannot be empty. '),
Array (' title ', ', ', ' title already exists. ', 0, ' Unique ', 1}
;}
In operations, data validation occurs automatically when a data object is created using the Create method (when the POST submits the form is processed):
$Article = D ("Article");
if (! $Article->create ()) {
//If the creation fails, indicating that validation did not pass (note: One of the possible causes) Output error message
exit ($Article->geterror ());
} else{
//Validation pass, continue next process such as writing data to data table
}
The system has built-in some common regular verification rules, including: require (must), email (mailbox format), URL (url address), currency (currency), number (digital), QQ (QQ number), Chinese (English characters), you can use directly.
For user experience, the error message returned by the $Article->geterror () is generally returned in AJAX mode, in detail in thinkphp Ajax. to define your own validation rules
When the built-in validation rules do not meet the validation requirements, you can define your own validation rules, such as requiring the date of birth to be entered in the 2000-1-1 format:
Array (' Birthday ', '/^d{4}-d{1,2}-d{1,2}$/', ' The birth date is malformed. '),
Visible, just a few lines of simple code, the implementation of a variety of different forms of form validation, flexible use of thinkphp automatic verification, can make the development process more than twice. For more automatic validation examples, see the "thinkphp Automatic validation Example" in this section.