[PHP] The use of TP5.1 validator to pass parameters in scene verification

little bottle
Release: 2023-04-05 22:42:02
forward
3302 people have browsed it

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',
Copy after login

, 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');
Copy after login

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!

Related labels:
source:cnblogs.com
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 Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template