The main content of this article is an introduction to the method of passing parameters in thinkphp5.1 validator scenario verification. It has certain reference significance. Interested friends can learn more
A scenario: The user saves his own nickname. If another user has already used this nickname, saving is not allowed, but the current user himself must be excluded, because if the user has not made any changes, the new nickname can be saved just like the old nickname. of.
Because the nickname defines the only rule:
'name' => 'unique:user',
, so even the current user cannot save the same nickname without modification.
Solution: In the controller, pass the uid (primary key) to the validator through the validator’s only channel $data parameter
//控制器代码 $data['uid'] = 10; $validate = $this->validate($data, 'app\common\validate\User.info');
In this way, the validator will automatically exclude the current primary key, and the current primary key can be saved smoothly. Not bound by uniqueness.
If you have encountered the same problem, please refer to it.
Related courses: PHP video tutorial
The above is the detailed content of [PHP] The use of TP5.1 validator to pass parameters in scene verification. For more information, please follow other related articles on the PHP Chinese website!