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:
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
3. Write the model file in thinkphp\Application\Home\Model, which is the verification method .
Copy after login
2. Dynamic verification
1. In Application\Home\Controller Writing method
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
3. Write the model file in thinkphp\Application\Home\Model.
Copy after login
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
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
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!