search
HomeBackend DevelopmentPHP TutorialAbout the solution to form verification and ajax verification problems in TP framework

There are two ways to verify tp data, one is static and the other is dynamic. Below, the editor will bring you ThinkPhp framework form verification and ajax verification issues. Friends who are interested can take a look.

Previous form verification was written in js, and the verification of the tp framework can also be used here. But comparing the two, js verification is better, because tp framework verification will run background code, so the running speed and efficiency will decrease.

Automatic verification is a data verification method provided by the ThinkPHP model layer, which can automatically perform data verification when using create to create a data object. The verification code must be written in the model layer, that is, the Model.

 There are two ways to verify data:

Static method: Define verification through the $_validate attribute in the model class rule. After the static method is defined, it can be used elsewhere.

Dynamic method: Use the validate method of the model class to dynamically create automatic validation rules. The dynamic method is more flexible. It can be written wherever it is used and cannot be used elsewhere.

No matter what method is used, the definition of verification rules is a unified rule, and the definition format is:


<?php
namespace 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. In thinkphp\Application\Home\View \Test writes the corresponding html file


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
</head>
<style type="text/css">
*{ font-family:微软雅黑; padding:0px; margin:0px auto}
</style>
<body>
<form action="__ACTION__" method="post">
<p>用户名:<input type="text" name="uid" /></p>
<p>密码:<input type="text" name="pwd" /></p>
<p>确认密码:<input type="text" name="pwd1" /></p>
<p>姓名:<input type="text" name="name" /></p>
<p>邮箱:<input type="text" name="email" /></p>
<p>年龄:<input type="text" name="age" /></p>
<p><input type="submit" value="提交" /></p>
</form>
</p>
</body>
</html>

3. Write the model file in thinkphp\Application\Home\Model, which is the verification method.


<?php
namespace Home\Model;
use Think\Model;
class YongHuuModel extends Model
{
protected $tablePrefix = "";
protected $trueTableName = &#39;yonghuu&#39;; //真实表名
//protected $patchValidate = true;
protected $_validate = array(
array(&#39;uid&#39;,&#39;require&#39;,&#39;用户名不能为空!&#39;),
array(&#39;pwd&#39;,&#39;pwd1&#39;,&#39;两次输入的密码不一致!&#39;,0,&#39;confirm&#39;), //两个字段是否相同
array(&#39;email&#39;,&#39;email&#39;,&#39;邮箱格式不正确&#39;),
array(&#39;name&#39;,&#39;/^[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)$/&#39;,&#39;身份证号不正确!&#39;,0,&#39;regex&#39;),
array(&#39;age&#39;,&#39;18,50&#39;,&#39;年龄不在范围内&#39;,0,&#39;between&#39;),
);
}

2. Dynamic verification

1. Write the method in Application\Home\Controller


<?php
namespace Home\Controller;
use Think\Controller;
class TestController extends Controller
{
  public function add()
  {
    if(empty($_POST))//如果post数组为空
    {
      $this->show();//显示add.html页面
    }
    else//如果post数组不为空
    {
      $y = D("YongHu");
      $arr = array(//动态验证就是需要在哪验证就在哪里写验证方法。
        array("uid","require","用户名不能为空",0),//讲验证的方法写在方法里面
      );
      if($y->validate($arr)->create())//这里要先调用validate方法,然后将写的验证方法放到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">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
<style type="text/css">
</style>
</head>
<body>
  <form action="__ACTION__" method="post">
    <p>用户名:<input type="text" name="uid" /></p>
    <p>密码:<input type="text" name="pwd" /></p>
    <p>确认密码:<input type="text" name="pwd1" /></p>
    <p>姓名:<input type="text" name="name" /></p>
    <p>邮箱:<input type="text" name="email" /></p>
    <p>年龄:<input type="text" name="age" /></p>
    <p><input type="submit" value="提交" /></p>
  </form>
</body>
<script type="text/javascript">
</script>
</html>

3 .Write the model file in thinkphp\Application\Home\Model.


<?php
namespace Home\Model;
use Think\Model;
class YongHuModel extends Model
{
  protected $tablePrefix = "";//表示表格前缀为空,就是没有前缀。
  protected $trueTableName = "yonghu";//如果不写这句话,会自动去找Yong_Hu这张表,这是默认的表格的命名。这里要写上实际的表格的名字。
}

3. Ajax verification

There is a big difference between tp dynamic verification and static verification The disadvantage is that when an error message is prompted, it must jump to other pages to display the error message. If you need to display an error message on the current page, you need to use ajax for verification.

1. Write display and ajax processing methods


<?php
namespace Home\Controller;
use Think\Controller;
class TestController extends Controller
{
  public function tianjia()//添加方法,用来显示页面
  {
    $this->show();
  }
  public function test()//ajax处理方法
  {
    $y = D("YongHu");
    $arr = array(//动态验证就是需要在哪验证就在哪里写验证方法。
        array("uid","require","用户名不能为空"),//讲验证的方法写在方法里面
      );
    if($y->validate($arr)->create())//这里要先调用validate方法,然后将写的验证方法放到validate里面
      {
        $this->ajaxReturn("通过验证","eval");
      }
      else
      {
        $this->ajaxReturn($y->getError(),"eval");
      }
  }
}

2. Write display page


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script src="__PUBLIC__/js/jquery-1.11.2.min.js"></script>
<title>无标题文档</title>
<style type="text/css">
</style>
</head>
<body>
    <p>用户名:<input id="uid" type="text" name="uid" /></p>
    <p><input id="btn" type="button" value="验证" /></p>
</body>
<script type="text/javascript">
  $("#btn").click(function(){
      var uid = $("#uid").val();
      $.ajax({
        url:"__CONTROLLER__/test",
        data:{uid:uid},
        type:"POST",
        dataType:"TEXT",
        success: function(data){
            alert(data);
          }        
        })
    })
</script>
</html>

Summarize

The above is the detailed content of About the solution to form verification and ajax verification problems in TP framework. For more information, please follow other related articles on the PHP Chinese website!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
golang中如何验证输入是否为大写字母golang中如何验证输入是否为大写字母Jun 24, 2023 am 09:06 AM

Golang是一门高性能、现代化的编程语言,在日常开发中经常涉及到字符串的处理。其中,验证输入是否为大写字母是一个常见的需求。本文将介绍在Golang中如何验证输入是否为大写字母。方法一:使用unicode包Golang中的unicode包提供了一系列函数来判断字符的编码类型。对于大写字母,其对应的编码范围为65-90(十进制),因此我们可以使用unicod

golang中如何验证输入是否为全角字符golang中如何验证输入是否为全角字符Jun 25, 2023 pm 02:03 PM

在golang中,验证输入是否为全角字符需要用到Unicode编码和rune类型。Unicode编码是一种将字符集中的每个字符分配一个唯一的数字码位的字符编码标准,其中包含了全角字符和半角字符。而rune类型是golang中用于表示Unicode字符的类型。第一步,需要将输入转换为rune类型的切片。这可以通过使用golang的[]rune类型进行转换,例如

PHP正则表达式验证特定字符串开头结尾的方法PHP正则表达式验证特定字符串开头结尾的方法Jun 24, 2023 am 11:20 AM

PHP是一种非常流行的编程语言,常用于Web开发。在PHP开发中,我们经常会遇到需要验证字符串的情况。其中,正则表达式是一种非常常用的方法。在对字符串进行验证时,我们经常需要验证字符串是否以特定字符或字符串开头或结尾。本文将介绍如何使用PHP正则表达式来验证字符串的开头或结尾。验证字符串开头在PHP中,通过正则表达式验证字符串开头,我们可以使用"^"符号来表

golang中如何验证输入是否全部为中文字符golang中如何验证输入是否全部为中文字符Jun 24, 2023 am 09:16 AM

随着时代的发展,我们越来越注重对数据的校验,特别是对用户输入的校验。对于语言类的校验,如何准确判定输入是否全部为中文字符成为了一个重要问题。而在golang中,我们可以借助unicode包和regexp包来实现这一需求。一、unicode包unicode包提供了一系列对于unicode的核心支持。我们可以使用这个包中的函数来准确地判断一个字符是否为中文字符。

在PHP中使用Google reCAPTCHA进行验证在PHP中使用Google reCAPTCHA进行验证Jun 19, 2023 pm 05:38 PM

在现代网络世界中,网站的安全性以及用户隐私的保护越来越成为重要话题。其中,人机验证这一技术方法已经成为防范恶意攻击行为的不可或缺的方式之一。GooglereCAPTCHA,是一个被广泛应用于人机验证的工具,其概念已经深入人心,甚至在我们每天使用的许多网站上都能够看到其存在的身影。在本文中,我们将探讨如何在PHP中使用GooglereCAPTCHA进行验证

手机号码验证登录注册的PHP实现指南手机号码验证登录注册的PHP实现指南Aug 17, 2023 pm 03:18 PM

手机号码验证登录注册的PHP实现指南一、概述手机号码验证是现代互联网应用中常见的功能之一,它不仅可以用于用户注册和登录验证,还可以用于短信验证码发送等场景。本文将介绍如何使用PHP语言实现手机号码验证登录注册功能。二、环境要求在开始编写代码之前,我们需要确保以下环境已经准备就绪:PHP环境:PHP的版本需达到5.6或以上。数据库:本文使用MySQL数据库作为

golang中如何验证输入是否为英文字母golang中如何验证输入是否为英文字母Jun 24, 2023 am 08:36 AM

作为一门语言,Golang提供了许多方法来方便我们进行数据的验证和处理。其中,验证输入是否为英文字母是一项基本的功能,本篇文章将介绍Golang中两种实现这一功能的方式。1.使用正则表达式正则表达式是一种可以匹配文本片段的表达式。在Golang中,我们可以使用标准库中的regexp包来处理和匹配正则表达式。下面是一个验证输入是否为英文字母的代码示

PHP正则表达式验证正整数的方法PHP正则表达式验证正整数的方法Jun 24, 2023 am 08:55 AM

在PHP中,正则表达式可以用于验证和处理字符串。验证正整数的正则表达式如下所示:$pattern=&quot;/^[1-9]d*$/&quot;;其中,^表示开头,$表示结尾,[1-9]表示第一个字符必须是1-9之间的数字,d表示其他字符必须是数字,*表示0个或多个。因此,这个正则表达式可以匹配任意一个正整数。下面是一个完整的例子,演示如何使用正则表达式

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Tools

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment