1. Introduction to ThinkPHP automatic verification
ThinkPHP automatic verification is a set of verification mechanisms built into the framework. It can set verification rules and errors in the model Prompt information, and perform data verification based on actual scenarios in the controller. Using automatic verification can avoid the tedious manual verification process and improve development efficiency. With automatic validation, error handling code can be reduced because it will directly return an error message when the data is illegal.
The basic usage of ThinkPHP automatic verification is as follows:
Define validation rules and error messages in the model:
protected $_validate = array( // 验证用户名是否合法 array('username','require','用户名不能为空!'), array('username','','该用户名已被注册',0,'unique'), array('username','/^[\w\-\x{4e00}-\x{9fa5}]{2,16}$/','用户名不合法!',0,'regex'), // 验证邮箱是否合法 array('email','require','电子邮箱不能为空!'), array('email','','该邮箱已被注册',0,'unique'), array('email','email','电子邮箱格式不正确!',0,'regex'), // 验证密码是否合法 array('password','require','密码不能为空!'), array('password','/^[\S]{6,32}$/','密码格式不正确!',0,'regex'), );
Data validation in the controller:
public function register(){ if(IS_POST){ $user = D('User'); if(!$user->create()){ $this->ajaxReturn(array('status'=>0,'msg'=>$user->getError())); }else{ $user->add(); $this->ajaxReturn(array('status'=>1,'msg'=>'注册成功!')); } } }
In the above code, we use $user->create()
Data verification, if the verification fails, use $user->getError()
to obtain the error information and return it to the front-end page. If the validation is successful, the data is added to the database.
2. ThinkPHP automatic verification modification
In the actual development process, sometimes some fields need to be updated, but in this case data also needs to be updated verify. Although we can use an automatic verification mechanism, this mechanism will verify all verification rules again, resulting in a waste of time and resources.
In order to solve this problem, ThinkPHP provides an automatic verification modification function, which can only verify the fields that need to be verified according to the current scenario. If you want to modify the username
and email
fields in the database without verifying password
, you can use the following code:
public function update(){ if(IS_POST){ $user = D('User'); $data = array( 'id' => $_POST['id'], 'username' => $_POST['username'], 'email' => $_POST['email'], ); if(!$user->create($data, 2)){ $this->ajaxReturn(array('status'=>0,'msg'=>$user->getError())); }else{ $user->save(); $this->ajaxReturn(array('status'=>1,'msg'=>'更新成功!')); } } }
In the above code, we passed the second parameter 2
, indicating that the current update scene is. In this way, in the create()
method, the framework will only verify username
and email
, but not other fields.
The above is the detailed content of How to use the modification function of ThinkPHP automatic verification. For more information, please follow other related articles on the PHP Chinese website!