thinkphp automatic Verification mechanism detailed _php example

Source: Internet
Author: User
Tags php template regular expression codeigniter zend framework

The thinkphp automatic verification mechanism is described in the example. Share to everyone for your reference, specific as follows:

Array (validation fields, validation rules, error prompts, [validation criteria, additional rules, verification time])

1. Verify Field

The name of the form field that needs to be validated, which is not necessarily database fields or some of the secondary fields of the form, such as Confirm password and authentication code, and so on. Validation fields can be set arbitrarily if there are individual validation rules and field-independent cases, such as expire expiration rules that are not related to form fields.

2. Validation rules

The rules for validation need to be combined with additional rules, and if additional rules for regular validation are used, the system also has built-in rules for regular validation that can be used directly as validation rules, including: Require fields must, email mailboxes, URL URL addresses, currency currencies, Number.

3. Error Tips

Message definition for validation failure

4. Verification conditions

Contains the following scenarios:

Model::exists_validate or 0 fields are validated (default)
Model::must_validate or 1 must verify
Validation when model::value_validate or 2 values are not empty

5. Additional Rules

Used with validation rules, including the following rules:

Regex regular validation, the defined validation rule is a regular expression (default)
function validation, defined validation rule is a function name
callback method validation, defined validation rules are a method of the current model class
Confirm Verify that two fields in the form are the same, and that the validation rule defined is a field name
Equal verifies that the value is equal to a value that is defined by the preceding validation rule
In validation is within a range, the validation rule defined must be an array
Length verifies that the validation rule defined can be a number (representing a fixed length) or a range of digits (for example, 3,12 represents a range from 3 to 12)
Between validation scope, defined validation rule representation range, can use string or array, such as 1,31 or arrays (1,31)
Expire verify that in the validity period, the defined validation rules represent the time range, and can be in time, for example, you can use 2012-1-15,2013-1-15 to indicate that the current submission is valid between 2013-1-15 and 2012-1-15, or you can use a timestamp definition
Ip_allow Verify that IP is allowed, the defined validation rules represent a list of allowed IP addresses, separated by commas, such as 201.12.2.5,201.12.2.6
Ip_deny Verify that IP is prohibited, the defined validation rules represent a list of banned IP addresses, separated by commas, such as 201.12.2.5,201.12.2.6
Unique validation is unique, and the system queries the database based on the current value of the field to determine if the same value exists.

6. Verification time

Model:: Model_insert or 1 when new data validation
Model:: Model_update or 2 when editing data validation
Model:: Model_both or 3 validation in all cases (default)

Example

Protected $_validate = Array (
  ' verify ', ' Require ', ' captcha must be! '),///By default, verify
  Array (' name ', ', ', ', ') name already exists! ', 0, ' unique ', 1,///In addition verify that the Name field is unique
  array (' Value ', Array (1,2,3), ' value is not the correct range! ', 2, ' in ',//when the value is not empty, determine whether the
  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 '),//Custom Function Validation password format
);

Must be combined with create ()

$User = D ("User"); Instantiate the User object if
(! $User->create ()) {
  //If the creation fails indicating that the validation did not pass the output error message
exit ($User->geterror ());
} else{
  //validation can be done by other data operations
}

Project instance

Auto-Verify protected $_validate = Array (' name ', ' Require ', ' name cannot be empty!) ', 1, ', 3, Array (' Tel ', ' require ', ' cell phone 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's front photo cannot be empty! ', 1, ', 1, Array (' Id_opposite ', ' require ', ' ID, ' the reverse photo can not be empty!) ', 1, ', 1, Array (' Id_handle ', ' require ', ' hand ID photo cannot be empty! ', 1, ', 1, Array (' Bankcard ', ' require ', ' bank card photo cannot be empty! ', 1, ', 1, Array (' Openbank ', ' require ', ' the bank 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}$/', ' Mobile number illegal! ', 1, ' Regex ', 3, Array (' email ', ' email ', ' email is illegal! ', 1, ', 3, Array (' Id_number ', '/^ \d{15}$|^\d{18}$|^\d{17} (\d| x|x)) $/', ' The ID number is illegal! ', 1, ' Regex ', 3, Array (' Tel ', ', ', ' cell phone number already exists!
   ', 1, ' unique ', 3),//Add change to verify that 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 only);

Description, as long as the definition of model, called anywhere, will be validated. Very convenient.

Must be the D method to be effective. The M method does not trigger automatic validation.

More interested readers of thinkphp related content can view the website topic: "thinkphp Introductory Course", "thinkphp template Operation Skill Summary", "thinkphp Common Method Summary", "The CodeIgniter Introductory Course", "CI ( CodeIgniter) Framework Advanced tutorials, introductory tutorials for the Zend framework Framework, Smarty Templates Primer tutorial, and PHP template technology summary.

I hope this article will help you with the PHP program design based on thinkphp framework.

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.