Detailed explanation of ThinkPhp framework form verification and ajax verification examples

小云云
Release: 2023-03-19 21:58:01
Original
1067 people have browsed it

There are two ways to verify tp data, one is static and the other is dynamic. The previous form verification was written in js. The verification of 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 methods of data validation:

Static method: Define validation rules through the $_validate attribute in the model class. 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:

show(); } else { $y=new \Home\Model\YongHuuModel(); $r=$y->create(); if($r) { $y->add(); } else{ die($y->getError()); } } } }
Copy after login

2. Write the corresponding html file in thinkphp\Application\Home\View\Test

    无标题文档 

用户名:

密码:

确认密码:

姓名:

邮箱:

年龄:

Copy after login

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


        
Copy after login

2. Dynamic verification

1. Write the method in Application\Home\Controller

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()); } } } }
Copy after login

2. Write the corresponding method in thinkphp\Application\Home\View\Test html file

    无标题文档  

用户名:

密码:

确认密码:

姓名:

邮箱:

年龄:

Copy after login

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


        
Copy after login

3. Ajax verification

tp dynamic verification and static verification both have a big shortcoming, that is, when an error message is prompted, they have to jump to other pages to display the error. information. 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

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"); } } }
Copy after login

2. Write display page

     无标题文档  

用户名:

Copy after login

Related recommendations:

PHP users Verify PHP+Ajax verification code to verify user login

Ajax verification username example code

ajax verification username and password, ajax verification username_ PHP tutorial

The above is the detailed content of Detailed explanation of ThinkPhp framework form verification and ajax verification examples. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
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
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!