In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly shows you the "thinkPHP automatic verification mechanism example analysis", the content is easy to understand, clear, hope to help you solve your doubts, the following let the editor lead you to study and study the "thinkPHP automatic verification mechanism example analysis" this article.
The details are as follows:
Array (validation field, validation rule, error prompt, [verification condition, additional rule, verification time])
1. Validate field
The name of the form field that needs to be validated, which is not necessarily a database field, but can also be some auxiliary fields of the form, such as confirmation password, CAPTCHA, and so on. In cases where individual validation rules are independent of the field, the validation field can be set at will, for example, the expire validity rule is independent of the form field.
two。 Verification rules
To validate rules, you need to combine additional rules. If you use additional rules for regular verification, the system also has some common regular verification rules built in, which can be directly used as verification rules, including: require field must, email mailbox, url URL address, currency currency, number number.
3. Error prompt
Used for the definition of prompt information after verification failure
4. Verification condition
It includes the following situations:
Validate if a field exists in Model::EXISTS_VALIDATE or 0 (default)
Model::MUST_VALIDATE or 1 must verify
Verify when the Model::VALUE_VALIDATE or 2 value is not empty
5. Additional rules
Used in conjunction with validation rules, including the following rules:
Regex regular validation, the validation rule defined is a regular expression (default)
Function function validation, the validation rule defined is a function name
Callback method validation, the validation rule defined is a method of the current model class
Confirm verifies that the two fields in the form are the same. The validation rule defined is a field name.
Equal verifies that it is equal to a value defined by the previous validation rule
In validation is within a certain range. The defined validation rules must be an array.
Length validation length, which can be defined as a number (representing a fixed length) or a range of numbers (for example, 3-12 represents a range of length from 3 to 12)
Between validation scope, which can be represented by defined validation rules. You can use a string or an array, such as 1mem31 or array (1mem31).
Expire verifies whether the validity period is valid. The defined verification rules indicate the time range, and the time can be reached. For example, 2012-1-15 can be used to indicate that the current submission is valid between 2012-1-15 and 2013-1-15, or a timestamp can be used to define it.
Ip_allow verifies whether IP is allowed, and the defined verification rules represent the list of allowed IP addresses, separated by commas, such as 201.12.2.5201.12.2.6
Ip_deny verifies whether IP is prohibited. The defined verification rules represent a list of prohibited ip addresses separated by commas, such as 201.12.2.5201.12.2.6
Unique verifies whether it is unique, and the system queries the database based on the current value of the field to determine whether the same value exists.
6. Verification time
Verify when Model:: MODEL_INSERT or 1 adds new data
Model:: MODEL_UPDATE or 2 verify when editing data
Model:: MODEL_BOTH or 3 validate in all cases (default)
Example
Protected $_ validate = array (array ('verify','require',' CAPTCHA must!') , / / verify array with rules by default ('name','',' account name already exists!' / / verify whether the name field is unique array ('value',array (1 ~ 2 ~ 3),' incorrect range of values!' , / / determine whether the array is within a range if the value is not empty ('repassword','password',' confirm password is incorrect', 0 'repassword','password',' password'), / / verify whether the password is consistent with the password ('incorrect password','checkPwd',' password format', 0 password','checkPwd',' password function), / / verify the password format by custom function)
Must be used with create ()
$User = D ("User"); / / instantiate User object if (! $User- > create ()) {/ / if creation fails, validation fails to output error message exit ($User- > getError ());} else {/ / verify that other data operations can be performed.
Project example
/ / automatically validate protected $_ validate = array (array ('name',' require', 'name cannot be empty!' , 1,', 3), array ('tel',' require', 'mobile number cannot be empty!' , 1,', 3), array ('password',' require', 'password cannot be empty!' , 1,', 1), array ('email',' require', 'mailbox cannot be empty!' , 1,', 3), array ('ID_number',' require','ID number cannot be empty!' , 1,', 3), array ('ID_positive',' require','ID card front photo cannot be empty!' , 1,', 1), array ('ID_opposite',' require','ID card negative photo cannot be empty!' , 1,', 1), array ('ID_handle',' require', 'handheld ID photo cannot be empty!' , 1,', 1), array ('bankcard',' require', 'bank card photo cannot be empty!' , 1,', 1), array ('openbank',' require', 'bank of deposit cannot be empty!' , 1,', 3), array ('accountname',' require', 'bank account name cannot be empty!' , 1,', 3), array ('bankaccount',' require', 'bank account number cannot be empty!' , 1,', 3), array ('tel',' / ^\ d {11} $/', 'invalid mobile number!' , 1, 'regex', 3), array (' email', 'email',' mailbox is illegal!' , 1,', 3), array ('ID_number',' / ^ (\ d {15} $| ^\ d {18} $| ^\ d {17} (\ d | X | x)) $/', 'invalid ID number!' , 1, 'regex', 3), array (' tel',', 'mobile number already exists!' , 1, 'unique', 3), / / when adding and modifying, verify whether the tel field is unique array (' email',', 'mailbox already exists!' , 1, 'unique', 3), / / email unique array (' ID_number',','ID number already exists!' , 1, 'unique', 3), / / ID number is unique)
Note that as long as Model is defined and called anywhere, it will be validated. It's convenient.
It must be D method to be effective. The M method does not trigger automatic validation.
The above is all the contents of the article "sample Analysis of thinkPHP automatic Verification Mechanism". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.