thinkphp Forms Automatic Validation application, thinkphp form validation _php tutorial

Source: Internet
Author: User

thinkphp form verification using, thinkphp form validation


Using the TP 3.2 framework

Public Function Add_post () {        ///Validation rule        $rule =array (            Array (' name ', ' Require ', ' Please enter name ', 1),//must verify name        );        $m =m (' user ');        Get Name,sex,contact data into the model and verify        if (! $m->field (' name,sex,contact ')->validate ($rule)->create ())            $ This->error ($m->geterror ());        $result = $m->add ();        if (! $result)            $this->error (' Add failed ');        $this->success (' Add Success ', U (' dir '));    }

Validation rules can also be written into the model, but I feel a little trouble, one is sometimes different page verification method will not be the same, the second is to see the code in this Add_post event, it is clear what data to receive, how to verify that the data can have a general understanding at the first glance, so summed up this way.


Thinkphp automatically verifies that the error message appears: What does _token_error_ mean?

thinkphp new version of the built-in form token verification function, can effectively prevent the form of remote submission and other security protection.

Form token validation related configuration parameters are: ' token_on ' =>true,//whether to turn on token authentication ' token_name ' = ' __hash__ ',//Token verification form hidden field name ' token_type ' = ' MD5 ' ,//token hash validation rule defaults to MD5 if the form token verification feature is turned on, the system automatically generates a hidden field with the name Token_name in the template file with the form, and its value is a hash string generated by the Token_type method, which implements automatic token validation for the form. Automatically generated hidden fields before the form form end flag, if you want to control the location of the hidden field, you can manually add an identity to the form page and the system will automatically replace it when the template is output. If the token validation feature is not required for individual forms when you turn on form token validation, you can add {__notoken__} on the form page, and the system ignores token validation for the current form. If more than one form exists on the page, it is recommended to add identities and ensure that only one form requires token validation. Model classes automatically perform form token validation operations while creating data objects, and you need to manually invoke the model's Autochecktoken method for form token validation If you are not creating a data object using the Create method. If False is returned, the form token validation error is indicated. For example: $User = M ("User"); Instantiate the User object//Manual token validation if (! $User->autochecktoken ($_post)) {//token validation error

thinkphp Auto-validation issues

show you an example I wrote:
//form validation
Protected $_validate=array (
//array (' Validation field ', ' Validation rule ', ' Error prompt ', validation criteria, additional rules , authentication time)
Array (' uname ', ' require ', ' username must be verified! ', 1, ' Regex ', 3),
//array (' username ', ', ' username already exists ', 1, ' unique ', 1),
Array (' pwd ', ' require ', ' password must be filled in! '),

Array (' pwd ', ' checkpwd ', ' password length not less than 6 bits ', 1, ' callback '),
);

Function Checkpwd () {
$password =$_post[' pwd '];
if (strlen ($password) >=6) {
return true;
} else {
return false;
}
}
//form Map
Protected $_map=array (
' uname ' = ' username ',
' pwd ' = ' password ',
);
AutoComplete
Protected $_auto=array (
//array (fill field, fill content, fill condition, additional rule) fill condition: 1, insert 2, update 3, all
Array (' reg_date ', ' GetDate ', 1, ' callback '),
Array (' Password ', ' MD5 ', 3, ' function '),
);
function GetDate () {
return date (' y-m-d h:i:s ');
}
 

http://www.bkjia.com/PHPjc/892523.html www.bkjia.com true http://www.bkjia.com/PHPjc/892523.html techarticle thinkphp form verification using, thinkphp form validation uses the TP 3.2 Framework public Function Add_post () {//Validation rule $rule =array (Array (' name ', ' Require ', ' Please enter name ', 1),//Must ...

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