thinkphp implementing forms Automatic Validation, thinkphp implementing forms _php Tutorials

Source: Internet
Author: User

thinkphp implementing forms Automatic Validation, thinkphp implementing forms


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.

Copy the Code code as follows:
Public Function checkverify ($verify) {

if (MD5 ($verify)! = Session::get (' verify ')) return false;
return true;
}
Automatic validation

Copy the Code code as follows:
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):

Copy the Code code as follows:
$validate = Array (
' Require ' = '/.+/',
' Email ' = '/^\w+ ' ([-+.] \w+) *@\w+ ([-.] \w+) *\.\w+ ([-.] \w+) *$/',
' url ' = = '/^http:\/\/[a-za-z0-9]+\. [a-za-z0-9]+[\/=\?%\ -&_~ ' @[\]\ ': +!] * ([^<>\ "]) *$/',
' Currency ' = '/^\d+ (\.\d+) $/',
' Number ' = '/^\d+$/',
' Zip ' = '/^[1-9]\d{5}$/',
' Integer ' = '/^[-\+]?\d+$/',
' Double ' = '/^[-\+]?\d+ ' (\.\d+)? $/',
' English ' = '/^[a-za-z]+$/',
);

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:

Copy the Code code as follows:
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
);

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/932491.html www.bkjia.com true http://www.bkjia.com/PHPjc/932491.html techarticle thinkphp Realization Form automatic verification, thinkphp implementation form 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 ...

  • 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.