Today we are here to learn how to do the verification in the TP framework.
There are two types of validation: static verification, dynamic validation
First, let's start with an HTML interface called add.html. The code is as follows:
<!DOCTYPE HTML Public "-//W3C//DTD XHTML 1.0 transitional//en" "http://www.w3.org/TR/xhtml1/DTD/ Xhtml1-transitional.dtd "><HTMLxmlns= "http://www.w3.org/1999/xhtml"><Head><Metahttp-equiv= "Content-type"content= "text/html; charset=utf-8" /><title>Untitled Document</title></Head><Body><formAction= "__action__"Method= "POST"> <Div>Account:<inputtype= "text"name= "UID" /></Div> <Div>Password:<inputtype= "text"name= "pwd" /></Div> <Div>Confirm Password:<inputtype= "text"name= "Pwd1" /></Div> <Div>Name:<inputtype= "text"name= "Name" /></Div> <Div>Mailbox:<inputtype= "text"name= "Email" /></Div> <Div>Age:<inputtype= "text"name= "Age" /></Div> <BR/> <Div><inputtype= "Submit"value= "Add" /></Div></form></Body></HTML>
Static validation: Written inside the Model class
In a static way, you have to write a method in the model
New file: YongHuModel.class.php
The code is as follows:
<?phpnamespace Home\model; UseThink\model;classYonghumodelextendsmodel{protected $tablePrefix= ""; protected $trueTableName= ' Yonghu ';//Real table name protected $_validate=Array( //validation is performed in code order Array(' uid ', ' require ', ' username cannot be empty! ‘),//Verify user name Array(' pwd ', ' pwd1 ', ' two times the password entered is inconsistent! ', 0, ' confirm '),//Verify that two fields are the same Array(' email ', ' email ', ' email ' format is incorrect! ‘),//Verify the mailbox format 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! ', ' regex '),
//validating rules with regex requires writing regular expressions Array(' Ages ', ' 18,50 ', ' age not within range ', 0, ' between ')//Verify age Range );}
And then we're going to do the backstage operation.
//Static Validation Public functionAdd () {if(Empty($_post)) { $this-Show (); } Else { $yh=New\home\model\yonghumodel (); $r=$yh->create ();//The Create method is automatically validated and verified by returning true if($r) { $yh->add ();//Add Success } Else { die($yh->geterror ());//add failed, return error message}
Static methods Let's take a look at the dynamic approach.
Dynamic methods: When to verify when to write
Dynamic methods we can only write backstage.
Public function Add ()
{
$yh = D ("Yonghu");
$arr=Array( Array(' uid ', ' require ', ' username cannot be empty! ‘),//Verify user name Array(' pwd ', ' pwd1 ', ' two times the password entered is inconsistent! ', 0, ' confirm '),//Verify that two fields are the same//if you want to perform batch validation, you need to set protected $patchValidate = True in the model class; ); if($yh->validate ($arr)->create ())//use the Validate method to add the verification in . { $yh-Add (); } Else { die($yh-GetError ()); } } }
When we get here, it's over.
However, in the development of the time we use the TP framework Ajax more, then how to verify with Ajax? Don't worry, we'll see what happens next.
Ajax Dynamic Validation
<?phpnamespace Home\controller; UseThink\controller;classTestControllerextendscontroller{//ajax Dynamic Validation Public functionTest () {$yh= D ("Yonghu");//Create a parent class $arr=Array( Array(' uid ', ' require ', ' username cannot be empty! ‘),//Validation ); if($arr) { $this->ajaxreturn ("Pass Verification", "Eval");//by verifying } Else { $this->ajaxreturn ($yh->geterror (), "eval");//return error message } $Verify=New\think\verify (); $Verify-entry (); } Public functionTianjia () {if(Empty($_post)) { $this->show ();//Show Page } Else { } }
over~~~
TP Framework Verification Information