thinkphp Framework Form validation and AJAX validation examples

Source: Internet
Author: User
Tags validation examples
TP data validation in two ways, one is static, one is dynamic, the previous form verification is JS write, here can also use the TP framework validation. However, the comparison between the two is better than JS verification, because the TP framework validation will run the background code, so that the speed and efficiency will be reduced. Automatic validation is a method of data validation provided by the Thinkphp model layer that automates data validation when creating data objects using create. The code to be validated is written in the model layer.

There are two ways of validating data:

Static mode: The validation rule is defined in the model class through the $_validate attribute. The static mode is defined so that it can be used elsewhere.

Dynamic: Use the Validate method of the model class to dynamically create automatic validation rules. Dynamic mode is more flexible, where the use of writing, other places can not be used.

In any way, the definition of a validation rule is a uniform rule that defines the format as:

<?phpnamespace home\controller;use think\controller;class TestController extends Controller{public function add () { if (empty ($_post)) {$this->show ();} else{$y =new \home\model\yonghuumodel (), $r = $y->create (), if ($r) {$y->add ();} Else{die ($y->geterror ());}}} }

2. Write the corresponding HTML file in Thinkphp\application\home\view\test

<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">

3. Write the model file in Thinkphp\application\home\model, which is the method of verification.

<?phpnamespace home\model;use think\model;class Yonghuumodel extends model{protected $tablePrefix = "";p rotected $ Truetablename = ' Yonghuu '; Real table name//protected $patchValidate = true;protected $_validate = Array (' UID ', ' require ', ' username cannot be empty! '), array (' pwd ', ' pwd1 ', ' two times the password entered is inconsistent! ', 0, ' confirm '),//two fields are the same as array (' email ', ' email ', ' Mailbox format incorrect '), array (' name ', '/^[1-9]\d{5}[1-9]\d{3} ((0\d) | ( 1[0-2]) (([0|1|2]\d) |3[0-1]) \d{3} ([0-9]| X) $/', ' ID number is incorrect! ', 0, ' regex '), Array (' Age ', ' 18,50 ', ' aged not within range ', 0, ' between '),);}

Second, dynamic verification

1. Write a method in Application\home\controller

<?phpnamespace home\controller;use Think\controller;class TestController extends controller{public  function Add ()  {if    (empty ($_post))//If the POST array is empty    {      $this->show ();//Display add.html page    }    else// If the post array is not empty    {      $y = D ("Yonghu");      $arr = Array (//dynamic validation is where you need to verify where to write the verification method.)        Array ("UID", "Require", "user name cannot be null", 0),//The method of verification is written in the method      );      if ($y->validate ($arr)->create ())//The Validate method is called first, then the written validation method is placed inside the validate      {        $y->add ();      }      else      {die        ($y->geterror ());      }    }}}  

2. Write the corresponding HTML file in Thinkphp\application\home\view\test

<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">

3. Write the model file inside the Thinkphp\application\home\model.

<?phpnamespace home\model;use think\model;class Yonghumodel extends model{  protected $tablePrefix = "";// Indicates that the table prefix is empty, that is, there is no prefix.  protected $trueTableName = "Yonghu";//If you do not write this sentence, will automatically go to find yong_hu this table, which is the default table naming. Here are the names of the actual tables. }

Third, Ajax to do verification

TP dynamic verification and static verification have a big disadvantage, that is, when the error message is prompted to jump to other pages to display the error message. If you need to display an error message on the current page, you need to validate it with Ajax.

1. Write display and Ajax processing methods

<?phpnamespace home\controller;use Think\controller;class TestController extends controller{public  function Tianjia ()//Add method to display page  {    $this->show ();  }  Public function test ()//ajax processing method  {    $y = D ("Yonghu");    $arr = Array (//dynamic validation is where you need to verify where to write the verification method.)        Array ("UID", "Require", "user name cannot be null"),//authentication method written in method      );    if ($y->validate ($arr)->create ())//The Validate method is called first, then the written validation method is placed inside the validate      {        $this- Ajaxreturn ("Pass Verification", "Eval");      else      {        $this->ajaxreturn ($y->geterror (), "eval");      }  }}

2. Write the Display page

<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">

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.