Automatic validation in thinkphp the previous article has an example

Source: Internet
Author: User

Note that, whenever a model is defined, it is invoked anywhere and will be validated. Very convenient.

Must be a D method before it is valid. The M method does not trigger automatic validation.

Array(

Array (Verify field 1, validation rules, error prompts, [validation conditions, additional rules, validation time]),

    Array (Verify field 2, validation rules, error prompts, [validation conditions, additional rules, validation time]),

.....

);

1. Validation fields

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

2. Validation rules

To validate the rules, you need to combine additional rules, if the use of regular validation of additional rules, the system also has built-in regular validation rules, can be used directly as a validation rule, including: Require field must, email mailbox, URL URL address, currency currency, Number.

3. Error message

Definition of the hint message for validation failure

4. Verification criteria

The following scenarios are included:

Model::exists_validate or 0 exists field is verified (default)

Model::must_validate or 1 must be verified

Model::value_validate or 2 value is not empty when validating

5. Additional Rules

Used with validation rules, including some of the following rules:

Regex regular validation, defined validation rule is a regular expression (default)

function validation, a defined validation rule is a function name

callback method validation, a defined validation rule is a method of the current model class

Confirm verify that the two fields in the form are the same, 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 verifies that a defined validation rule must be an array within a range

Length, the validation rule defined can be a number (representing a fixed length) or a range of numbers (for example, 3,12 indicates a range from 3 to 12)

Between validation scope, defined validation rule representation scope, can use strings or arrays, such as 1,31 or array (1,31)

Expire verify that the validation rules defined in the validity period represent a time range that can be used to time, for example, you can use 2012-1-15,2013-1-15 to indicate that the current commit is valid between 2012-1-15 and 2013-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, for example 201.12.2.5,201.12.2.6

Ip_deny Verify that IP is prohibited, the defined validation rules represent a list of forbidden IP addresses, separated by commas, such as 201.12.2.5,201.12.2.6

Unique authentication is only, the system will query the database based on the current value of the field to determine whether the same value exists.

6. Verification time

Model:: Model_insert or 1 when new data is validated

Model:: Model_update or 2 validation when editing data

Model:: Model_both or 3 verification in all cases (default)

//Automatic Validation    protected $_validate=Array (        Array(' name ', ' Require ', ' name cannot be empty! ', 1, ', 3),Array(' tel ', ' require ', ' mobile phone number cannot be empty! ', 1, ', 3),Array(' Password ', ' Require ', ' password cannot be empty! ', 1, ', 1),Array(' email ', ' require ', ' Mailbox can't be empty! ', 1, ', 3),Array(' Id_number ', ' require ', ' ID number can't be empty! ', 1, ', 3),Array(' id_positive ', ' require ', ' id ' front photo can't be empty! ', 1, ', 1),Array(' Id_opposite ', ' require ', ' ID card can not be empty! ', 1, ', 1),Array(' Id_handle ', ' require ', ' handheld ID can't be empty! ', 1, ', 1),Array(' Bankcard ', ' require ', ' bank card photos cannot be empty! ', 1, ', 1),Array(' Openbank ', ' require ', ' account opening line 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 phone number is not legal! ', 1, ' Regex ', 3),Array(' email ', ' email ', ' email ' is illegal! ', 1, ', 3),Array(' Id_number ', '/^ (\d{15}$|^\d{18}$|^\d{17} (\d| x|x) $/', ' ID number is not legal! ', 1, ' Regex ', 3),Array(' Tel ', ' ', ' mobile number already exists! ', 1, ' unique ', 3),//New modification verifies that the Tel field is unique        Array(' email ', ' ', ' Mailbox already exists! ', 1, ' unique ', 3),//Email only        Array(' Id_number ', ', ' ID number already exists! ', 1, ' unique ', 3),//ID number Unique);

Automatic validation in thinkphp the previous article has an example

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.