Example of automatic ThinkPHP form verification, thinkphp example _ PHP Tutorial

Source: Internet
Author: User
ThinkPHP form automatic verification example, thinkphp example. ThinkPHP form automatic verification example, thinkphp example uses TP3.2 framework publicfunctionadd_post () {verification rule $ rulearray (array (name, require, please enter name, 1 ), the ThinkPHP form must be used for automatic verification.

Use the TP 3.2 Framework

Public function add_post () {// verification rule $ rule = array ('name', 'require ', 'Enter name', 1 ), // name must be verified); $ m = M ('user'); // Obtain the name, sex, and contact data to 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 ('added successfully', U ('dir '));}

Verification rules can also be written to the model, but I feel a little troublesome. first, sometimes different page verification methods are used, and second, the code in this add_post event is displayed, it is clear what data to receive and how to verify that the data can be roughly understood at the first glance, so this method is summarized.


Isn't form verification of thinkphp ajax? Is there a better form verification tool than thinkphp? For example, jquery form

ThinkPHP's background framework does not have ajax itself. It is easy to submit with jquery form. You can take a look at the official thinkphp example.
Www.thinkphp1.cn/extend/230.html

Automatic Verification of thinkphp

Let me show you an example:
// Form verification
Protected $ _ validate = array (
// Array ('verification field', 'verification rule', 'error message', verification criteria, additional rules, verification time)
Array ('uname', 'require ', 'user name must be verified! ', 1, 'regex', 3 ),
// Array ('username', '', 'username already exists ', 1, 'Unique', 1 ),
Array ('pwd', 'require ', 'password is required! '),

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 ing
Protected $ _ map = array (
'Uname' => 'username ',
'Pwd' => 'password ',
);
// Automatic completion
Protected $ _ auto = array (
// Array (fill field, fill content, fill condition, add 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 ');
}

Example of automatic http://www.bkjia.com/PHPjc/893423.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/893423.htmlTechArticleThinkPHP form verification application, thinkphp example using TP 3.2 Framework public function add_post () {// validation rules $ rule = array ('name', 'require ', 'enter the name', 1), // must be verified...

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.