thinkphp automatic validation of related issues

Source: Internet
Author: User
thinkphp Automatic validation issues
Where is the function verification required to automatically verify the thinkphp registration?
Where do you write the validation function and how do I call it?

------Solution--------------------
The official documentation is clear:


thinkphp

5.3.15 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, supported by the validation factor format:


PHP code


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



Validation field: 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 confirmation password and verification code, and so on. Must

Validation rules: Rules to be validated, which need to be combined with additional rules (required)

Hint message: The definition of the message that was used to verify the failure (required)

Validation criteria: (optional)


PHP code


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

Model::must_to_validate or 1 must be verified

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



Additional rules: Use with validation rules (optional), including:

? The Regex uses regular validation to indicate that the validation rule defined earlier is a regular expression (default)

? function validation is used by functions, and the validation rule defined above is a functional name

? Callback using method validation, the previously 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 above is a field name

? Equal verifies that the value is equal to a value that is defined by the preceding validation rule

? In verify that the validation rule defined above must be an array within a range

? Unique authentication, the system will query the database based on the current value of the field to determine if the same value exists

The system also includes some common rules for regular validation, which can be used directly, including: Require field must, email mailbox, URL URL address, currency currency, number numbers, these validation rules can be used directly.

Verification time: (optional)


PHP code


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)



Example:


PHP code


Protected $_validate = Array (

Array (' Verify ', ' Require ', ' captcha must! '),//with regular validation by default

Array (name, ' ', ' account name already exists! ', 0, ' unique ', 1),//Verify that the Name field is unique when added

The range of array (' value ', array (All-in-all), ' value ' is incorrect! ', 2, ' in '),//When the value is not empty, determine if it is within 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

);



Data validation operations are performed automatically when a data object is created using the system's create method, and the code example:


PHP code


$User = D ("User"); Instantiating a User object

if (! $User->create ()) {

If the creation fails, the validation does not pass the output error message

Exit ($User->geterror ());

}else{

Verify that other data operations can be performed by

}



In general, the validation rules for each data table are relatively fixed, but there are special cases where the validation rules may change, and we can dynamically change the validation rules to meet the validation under different conditions:


PHP code


$User = D ("User"); Instantiating a User object

$validate = Array (

Array (' Verify ', ' Require ', ' captcha must! '),//Only verification of verification code is required

);

$User-SetProperty ("_validate", $validate);

$result = $User->create ();

if (! $result) {

If the creation fails, the validation does not pass the output error message

Exit ($User->geterror ());

}else{

Verify that other data operations can be performed by

}

------Solution--------------------
discuss
I know that, but I don't know where the function is, where the CHECKPWD function is placed,
Array (' Password ', ' checkpwd ', ' Password format incorrect ', 0, ' function '),//Custom function Validation password format
  • 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.