thinkphp form Auto-validate function, thinkphp form validation _php tutorial

Source: Internet
Author: User

thinkphp form Auto-validate function, thinkphp form verification


Last night, our boss asked me to get the form of automatic verification function, sorrow for a long day borrowed a lot of knowledge of the official website, just out, eh, to share my own results!

Thinkphp in the model base class for us to define the function and regular expression of auto-validation, we just need to build the $_validate attribute under the model class of the corresponding database table.

1, we find the model base class, you can see protected $_validate = Array (); Automatic verification, it is an array type, the following in the corresponding data model file defines it;

2.

function checkverify ($verify) {   ifreturnfalse;         return true ;}

Automatic validation

   Protected $_validate = Array (        array ("title", "Require", "title must!") "),        Array (' categoryId ', ' Require '," category must! "),        Array (' content ', ' require '," contents must! "),        Array (' verify ', ' Require ', ' captcha must! '),        Array (' verify ', ' checkverify ', ' CAPTCHA error! ', 0, ' callback ')    );


3. Format Description: Array (validation fields, validation rules, error prompts, validation conditions, additional rules, validation time),

4. Parameter explanation:

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.
Validation rules: The rules to be validated need to be combined with additional rules (must), and the official already contains the following rules (which you can add yourself):

 1 $validate =  Array ( 2 3 ' require ' = '/.+/' ,  4 5 ' Email ' = '/^\w+ ' ([-+.] \w+) *@\w+ ([-.] \w+) *\.\w+ ([-.] \w+) *$/' ,  6 7 ' url ' = = '/^http:\/\/[a-za-z0-9]+\. [a-za-z0-9]+[\/=\?%\ -&_~ ' @[\]\ ': +!] * ([^<>\ "]) *$/' ,  8 9 ' currency ' = '/^\d+ (\.\d+)? ' $/' ,  ten ' n Umber ' + '/^\d+$/' ,  ' zip ' = '/^[1-9]\d{5}$/' ,  ' integer '             ' = ' + '/^[-\+]?\d+$/' ,  "double ' + '/^[-\+]?\d+ (\.\d+)? $/' ,  18 19 ' 中文版 ' = '/^[a-za-z]+$/' ,  20 21); 

Hint message: The definition of the message that was used to verify the failure (must),
Validation criteria: (optional)
There are three rules for verifying conditions:
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:
Regex regular validation, stating that the previous validation rule is a regular expression;
function validation is used to indicate that the previous validation is a functional name;
Callback using method validation, it shows that the validation rule is a model class method;
Confirm verify that the two fields in the form are equal, and that the validation rule is a field name;
Equal verify that the value is equal to a value that is defined by the previous validation rule;
In verifies whether within a range, the preceding definition is an array;
Unique authentication is unique, the system will query the database according to the current value of the field to determine when the same value exists;
At the same time, the system also built up some common regular validation rules, which can be used in this section, including: Require field must, email mailbox;
Currency currency, number numbers, these validation rules can be used directly;

Verification time: (optional)

01.Model:: Model_insert or 1 when new data is added to verify
02.Model:: Model_update or 2 edit data when validating
03.Model:: Model_both or 3 verification in all cases (default)

5. Official Example:

protected $_validate  =  Array (   array (/ / with regular validation     by default// In addition, verify that the Name field is   unique to the array (' Value ', array (all-in-one), ' value ' range is incorrect! ', 2, '    in//when the value is not empty, determine if the password is in a range //  Verify that it is   consistent with the password Array (' Password ', ' checkpwd ', ' Password format incorrect ', 0, ' function')//  Custom Function Validation password format   );

Also do not know in line with our boss's request no, in short, it is out, good hard ah!! Big night to find code, find cases, seek comfort!!

http://www.bkjia.com/PHPjc/930046.html www.bkjia.com true http://www.bkjia.com/PHPjc/930046.html techarticle thinkphp Form automatic verification function, thinkphp form verification last night, our boss asked me to get the form of automatic verification function, sorrow for a half day borrowed a lot of knowledge of the official website, just out, eh, total ...

  • Related Article

    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.