thinkphp form Auto-validate instance, thinkphp form Instance _php Tutorial

Source: Internet
Author: User

thinkphp form Auto-validate instance, thinkphp form instance


In this paper, the implementation method of automatic verification of thinkphp forms is described. Share to everyone for your reference. Here's how:

The sample code is implemented using the TP 3.2 framework. The specific code is as follows:

Copy the Code Code as follows: Public function add_post () {
Validation rules
$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 '));
}

One of the validation rules can also be written into the model, but I feel some trouble, one is sometimes different page verification of the way 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.

It is hoped that this article will be helpful to everyone's thinkphp program development.


thinkphp Auto-validation issues

Here's an example of what I wrote:
Form validation
Protected $_validate=array (
Array (' Validation field ', ' Validation rule ', ' ERROR hint ', validation condition, additional rule, validation 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 Mappings
Protected $_map=array (
' uname ' = ' username ',
' pwd ' = ' password ',
);
Auto-complete function
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 ');
}

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

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

Form token validation related configuration parameters are: ' token_on ' =>true,//whether to turn on token validation ' token_name ' = ' __hash__ ',//token-Validated form-hidden field name ' Token_type ' = > ' MD5 ',//token hash validation rule defaults to MD5 if the form token validation feature is turned on, the system automatically generates a hidden field with the Token_name name in the template file with the form, whose value is the hash string generated by the Token_type method. Automatic token validation that is used to implement 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//Manually perform token validation if (! $User->autochecktoken ($_post)) {//token validation error
 

http://www.bkjia.com/PHPjc/893425.html www.bkjia.com true http://www.bkjia.com/PHPjc/893425.html techarticle thinkphp Form Auto-validation instance, thinkphp form instance This paper describes the implementation method of automatic verification of thinkphp forms. Share to everyone for your reference. Here's how it works: ...

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