I. Overview
The official term for thinkphp is automatic validation , Auto-completion , and field mapping , since these three functions are automatically implemented using the Create () method, so the individual is called the three-automatic, automatic verification , Auto-complete (auto-fill) , and auto-map
Second, automatic verification
Type checking is only for database-level validation, so the system also includes automatic validation of data Objects to complete the business rule validation of the model, and in most cases the data object is created by the $_post data submitted by the form. Need to use the system's automatic verification function, only need to define the $_validate attribute within the model class, is an array of multiple validation factors, a validation factor is a one-dimensional array, that is defined in the Custom model class $_ The Validate property is a two-dimensional array
Emphasize that automatic validation is for the Name property of the form, not the data object, or the Actual data table field. The corresponding fields in the data object are consistent with the corresponding properties in the data table!
The so-called automatic authentication, is to be based on the user's input (usually post form) to create a data object , automatically verify the data passed (such as the length of the user name, duplicate password and password is consistent, the mailbox format is legitimate, etc.) to obtain legitimate data. Usually these operations require manual (in the Controller) write code for processing, but thinkphp provides us with a better solution that is automatically implemented in the Create () Method!
1. Validation factor format
Array (validation fields, validation rules, error prompts, validation criteria, additional rules, validation time)
• Validation fields (required)
If the automatic mapping is not done, then the validation field must be the Name property value of the form, and if auto-mapping is done, then the validation field is the value after the mapping! In particular, it is important to note that automatically mapping to a value (which is not the same as the field name of the data table) does not affect automatic validation, but it affects the creation of the data object (where the creation of the data object does not mean that the creation failed but that the automatically mapped value that is not the same as the table field is not pressed into the That is, the data object is incomplete), so in general, the automatically mapped value is the actual field name of the data table, in order to facilitate the creation of data Objects!
Verify that the field does not have to be stored in the data table, or it can be some form of assistance, such as confirmation password, verification code, etc.
• Validation rules (required)
Rules to be validated, combined with additional rules
• Error notification (required)
Informational message for validation failure
• Verification criteria (optional)
Model::exists_to_vailidate or 0, which indicates that there is a validation (default). That is, if the field exists, verify that
Model::must_to_validate or 1, must be verified. That is, regardless of whether the field exists, it must be verified
Model::value_to_vailidate or 2, the value is not empty when the validation. That is, the data passed in the field is not empty to validate.
• Additional rules (optional)
Working with validation rules
The regex indicates that validation is done using regular, then the previous validation rule must be a regular expression (default)
There are regular rules built into the system that can be used directly, such as require (cannot be empty), email (email), url (URL), currency (currency), number (numeric)
You can also customize regular expressions or Google
function means that the validation is done using functions, then the previous validation rule must be a function
Callback indicates the use of methods for validation, then the previous validation rule must be a method of the current model class
Confirm to verify that two field values are the same, then the previous validation rule must be a field
Equal indicates whether the validation equals a value, the preceding validation rule must be a value
In indicates whether validation is within a range, the preceding validation rule must be an array
Unique indicates whether the authentication is unique, the system will query the database according to the value passed, if present, the error and terminate
• Verification time (optional)
Model:: Model_insert or 1, indicates when new data is validated
Model:: Model_update or 2, indicating when editing data is verified
Model:: Model_both or 3, which means verification in all cases (default)
2, all validation factor verification success is verified successfully, and the validation is successful before creating a data object (created successfully returned an array), otherwise not created (create failed to return FALSE)!
3, the error is obtained through the $model->geterror () method
4, in general, a table validation rules are fixed, but some special cases also need to change the validation rules, but for security reasons, the $_validate property is usually defined as private or protected, can not be accessed directly through the object. In view of this, thinkphp designed a public setproperty () method to modify the object properties
Obviously, if the $_validate property is public, it can be modified directly, but it is generally not defined as public!
The following is the definition of the SetProperty () method in the model class of the thinkphp source code, where the Property_exists () function (native PHP function) detects whether an object or class has a property
III. Automatic completion (auto-fill)
Auto-complete (auto-fill) is to automatically add additional attributes to the data object or post-process the existing attributes (for example, MD5 encryption of the password) after the data object is created based on the user's input (usually a form that is posted). To fit the actual data table field. Because some data table fields (such as user registration time, registered IP) values cannot or do not have to be entered by the user, but can or must be generated by the system. Usually these system-generated values or subsequent processing need to be manually (in the controller) to write code for processing, but thinkphp provides us with a better solution that is automatically implemented in the Create () Method!
The $_auto property is an array of multiple fill factors, and a fill factor is an array, so the $_auto property is a two-dimensional array
1. Fill factor format
Array (fill field, fill content, fill condition, attach rule)
• Fill Fields
The fill field can theoretically be any value, but in real development there are generally two cases, one of which is already in the data object (since auto-completion is after creating the data object)for subsequent processing (for example, MD5 encryption processing); The data object does not have an actual field in the data table (the exact point, which should be a non-nullable field), to add properties that can be inserted into the data table for that data Object!
differs from the validation field for automatic validation
As you can see from the first scenario, auto-completion can actually overwrite the submitted items of the form!
• Fill content (fill rule)
Also must be used in conjunction with additional rules
• Fill Conditions
Validation time meaning with validation factor
Model:: Model_insert or 1 when new data is processed (default)
Model:: model_update or 2 Update data processing
Model:: Model_both or 3 all cases are handled
• Additional Rules
function means that the fill content is a
Callback indicates that the fill content is a method of the current model class
field indicates that the fill is the value of one of the other fields
String indicates that the fill content is a string (default)
2, as with automatic verification, it is also possible to setproperty () method to dynamically modify the auto-completion rules
3. Unlike auto-validation, the auto-complete function or callback return value is the fill of the fill factor, while the automatically validated function or callback return value is a Boolean value
Iv. Automatic Mapping
Automatic mapping is the automatic mapping of the Name property value of the form-related control to the corresponding field name in the data table, in order to create the data object, before the data object is created based on the user's input (usually a form that comes in post). This element is not added to the data object if the element key in the array used to create the data object (usually $_post) cannot find the same field name in the actual data table. Usually this mapping operation needs to be manually (in the controller) to write code for processing (see below), but Thinkphp also provides us with a better solution that is automatically implemented in the Create () Method!
Some people would say, what's the trouble? Just write the Name property value in the form just like the actual field name in the datasheet. In fact, this practice is possible, but not safe! Because of this, the user can know the actual field name in the data table by the Name property value, which is undesirable from a security point of view!
thinkphp Tutorial _php Framework thinkphp (10) "Three big Automatic"