PHP---TP framework---form validation

Source: Internet
Author: User

Automatic validation is a method of data validation provided by the Thinkphp model layer that automates data validation when creating data objects using create. It is divided into static verification and dynamic verification.

For the basics, see the manual "Automatic Validation" chapter.

First, static verification

(1) Create a new Yanzheng controller under the ceshi/controller/path.

YanzhengController.class.php page

Note: Static definition mode because the model class must be defined, you can only instantiate the model with the D function

The Create method is the automatic validation of the post data submitted by the form

<?phpnamespace ceshi\controller;use think\controller;class Yanzhengcontroller extends  Controller{   Public Function Jtyz () {      $n = D ("users");   if (empty ($_post)) {      $this->show ();      } else{      //Verify to write on the model layer      if ($n->create ()) {      echo "validation passed!) ";      } else{      Echo $n->geterror ();  Get error message      }}      } public  function _empty () {   echo "the action method you have accessed does not exist!" ";   } }

(2) Make jtyz.html page in view layer

<! DOCTYPE html>

(3) Verification of static validation on the model layer: (PATH)

UsersModel.class.php

<?phpnamespace ceshi\model;use think\model;class Usersmodel extends model{protected $_validate = Array (     //non-null authentication Array ("UID", "Require", "User name cannot be empty"),//Equality Authentication Array ("PWD1", "Require", "password cannot be null"), Array ("Pwd2", "Pwd1", "two times password input inconsistent", "0", " Confirm "),//range validation  Array (" Age "," 18,30 "," aged not in range "," 0 "," between "),//Regular authentication  array (" email "," email "," Mailbox format is incorrect "),  );}

  

Verify in turn:

When all is empty, click Verify

Tips:

When the user name is entered, click Verify:

Show:

Two times the password input inconsistent, will prompt, the age is not within the range will prompt, the mailbox format is not correct when prompted; otherwise you will be prompted to verify the pass.

Second, dynamic verification

(1) YanzhengController.class.php page

<?phpnamespace ceshi\controller;use think\controller;class Yanzhengcontroller extends  Controller{//non-null verification static// Public   function Jtyz () {   //   $n = D ("users")////   if (empty ($_post)) {////   $this->show () ////   }else{  ////    verification to be written in the model layer//      if ($n->create ()) {//      echo "Verification passed! ";//      }else{//      echo $n->geterror ();  Get error message//      }   //   }//}//dynamic Public Function Dtyz () {         if (empty ($_post)) {   $this->show ();   }   Else   {      //validation rule       $rules = array (                    ' uid ', ' require ', ' username cannot be empty! '),                 );  $u = M ("users");          if ($u->validate ($rules)->create ()) {      $this->ajaxreturn ("OK", "eval");//If successful      }else{      $ This->ajaxreturn ($u->geterror (), "eval");  If not successful      }}      } public  function _empty () {   echo "the action method you are accessing does not exist!" ";   } }

(2) dtyz.html page:

<! DOCTYPE html>

Look at the effect:

When the text box loses focus:

When the text box has content, you lose focus again:

PHP---TP framework---form validation

Related Article

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.