Home >Backend Development >PHP Tutorial >Analysis on ThinkPhp framework form validation and ajax validation issues

Analysis on ThinkPhp framework form validation and ajax validation issues

不言
不言Original
2018-06-08 15:03:521320browse

This article mainly introduces the analysis of the ThinkPhp framework form verification and ajax verification issues. It has a certain reference value. Now I share it with you. Friends in need can refer to it.

There are two types of tp data verification There are two methods, one is static method and the other is dynamic method. The following brings you the ThinkPhp framework form verification and ajax verification issues. Friends who are interested should take a look.

Previous form verifications are all Written in js, you can also use the verification of the tp framework 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. In Application\Home\Controller Writing method

<?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

tp both dynamic verification and static verification A big shortcoming 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>

The above is the entire content of this article. I hope it will be helpful to everyone's study. For more related content, please pay attention to the PHP Chinese website!

Related recommendations:

ThinkPHP3.2 framework uses addAll() to insert data in batches

thinkPHP5 ajax submission Form operation

Code implemented by combining thinkphp with redis and queue

##

The above is the detailed content of Analysis on ThinkPhp framework form validation and ajax validation issues. 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