THINKPHP_ Dynamic Validation Validate ()

Source: Internet
Author: User

$data =i (' Post. ', ', ' trim ');
$rules = Array (
     array (' name ', ' ', ' data name already exists ', 0, ' unique ', 1),
    / /array (Validation field 1, validation rules, error prompts, [validation conditions, additional rules, validation time]),
     Array (' Verify ', ' Require ', ' captcha must! '),//By default with regular validation
     array (' name ', ' ', ' account name already exists! ', 0, ' unique ', 1),//In addition, verify that the Name field is unique
     array (' value ', ' array ', ' value ' range is incorrect!) ', 2, ' in '),//When the value is not empty to determine whether in a range
     array (' Repassword ', ' Password ', ' Confirm password incorrect ', 0, ' confirm ') ,//Verify that the password is consistent with the password
     array (' Password ', ' checkpwd ', ' Password format incorrect ', 0, ' function '),//Custom function Validation password format
);
if (! M (' data ')->validate ($rules)->create ($data) {
    $this->error (M (' data ')->geterror ());
}  

Validation Rule list:

Regex

Regular validation, the defined validation rule is a regular expression (default)

function

function validation, the 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, and that the validation rule defined is a field name

Equal

Verify that the value is equal to a value that is defined by the preceding validation rule

NotEqual

Verify that the value is not equal to a value that is defined by the previous validation rule (new in the 3.1.2 version)

Inch

Verifies whether a defined validation rule can be an array or a comma-delimited string within a range

Notin

Verify that the validation rule defined is not within a range, either an array or a comma-delimited string (3.1.2 version added)

Length

Validation length, the defined validation rule 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 scopes, defined validation rules representing ranges, can use strings or arrays, such as 1,31 or array (1,31)

Notbetween

Validation is not in a range, defined validation rules represent scope, can use strings or arrays (3.1.2 version added)

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, for example 201.12.2.5,201.12.2.6

Unique

Validation is unique, the system will query the database based on the current value of the field to determine if the same value exists, and when the form data contains a primary key field, unique cannot be used to determine the primary key field itself

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.