thinkphp Framework Implementation Form validation function code

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. This article mainly and everyone introduces thinkphp framework form verification operation method, the need for friends can refer to, hope to help everyone.

First, static verification

(1) Create a new index controller under the home/controller/path. Indexcontroller

IndexController.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 home\controller;use Think\controller;class Indexcontroller extends Controller {public    function Yanzheng () {    $u = D ("users");//Create a Subclass object    if (empty ($_post)) {      $this->show ();    } else{      if ($u->create ()) {//Verify        echo "Verification passed";      } else{        Echo $u->geterror ();//Get Error Message}}}  }

(2) Do yanzheng.html page under the View/index folder

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

:

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

UsersModel.class.php

<?phpnamespace home\model;use think\model;class Usersmodel extends model{    //add validation conditions    protected $_validate = Array (        "UID", "Require", "User name cannot be empty!"), and//is verified by default with a regular      array ("PWD1", "Require", "Password cannot be empty!"),      Array (" Pwd2 "," Require "," Password cannot be empty! "),         Array (" Pwd2 "," pwd1 "," password inconsistent two times entered ", 0," confirm "),//Verify that the password is consistent with the password      array (" Age ", "18,50", "Age not within range", 0, "between"),      Array ("email", "email", "email format incorrect"),    );}

Verify in turn:

When all is empty, click Verify

Will jump

Enter user name, other do not enter, will jump

Two times password input inconsistent, will prompt, age is not within the range will prompt, mailbox format is incorrect when prompted;

After entering the correct format content

Second, dynamic verification

(1) IndexController.class.php page

<?phpnamespace home\controller;use Think\controller;class Indexcontroller extends Controller {public    function YZ () {    $u = M ("users");//Create a parent class object    if (empty ($_post)) {      $this->show ();    } else{      $rules = Array (        "UID", "Require", "User name cannot be empty!"),      );      if ($u->validate ($rules)->create ()) {//verify        $this->ajaxreturn ("OK", "eval");      } else{        $this->ajaxreturn ("No", "eval");}}}  

(2) yz.html page:

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

Look at the effect:

When the text box loses focus:

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

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.