Home > Backend Development > PHP Tutorial > How to solve the problem of unique verification when editing in thinkphp5?

How to solve the problem of unique verification when editing in thinkphp5?

不言
Release: 2023-04-04 10:04:02
forward
3009 people have browsed it

本篇文章给大家带来的内容是关于thinkphp5编辑时验证唯一如何解决? 有一定的参考价值,有需要的朋友可以参考一下,希望对你有所帮助。

若定义了相关的验证规则,如:

namespace app\seller\validate;
use think\Validate;
class Goodsmtag extends Validate
{
    protected $rule = [
        'name'  =>  'require|max:25|unique:goodsmtag',
        'sort'  =>  'require|number|unique:goodsmtag',
        'pic'   =>  'require',
    ];

    protected $message  =   [
        'name.require' => '类别名称必须填写',
        'name.max' => '类别名称长度不得大于25位',
        'name.unique' => '类别名称不得重复',
        'sort.require' => '排序数字必须填写',
        'sort.number' => '排序必须为数字',
        'sort.unique' => '排序数字不得重复',
        'img.require' => '必须上传图片',
    ];

    protected $scene = [
        'add'  =>  [
            'name'=>'require|max:25|unique:goodsmtag',
            'sort'=>'require|number|unique:goodsmtag',
            'img'=>'require',
        ],
        'edit'  =>  [
            'name'=>'require|max:25|unique:goodsmtag',
            'sort'=>'require|number|unique:goodsmtag',
        ],
    ];
}
Copy after login

在添加界面验证的时候,提交自身的数据验证正常。
然而在编辑修改的页面验证时,提交自身数据会提示数据重复。

解决方法:
在编辑页面表单把主键id也作为数据传入到验证器中(name要和主键同名)。如:

How to solve the problem of unique verification when editing in thinkphp5?

 public function editOne($id){
        $data = [
            'name' => input('name'),
            'isshow' => $_POST['isshow'],
            'create_time' => time(),
            'sort' => input('sort'),
            'id' => input('id')
        ];
        $file = request()->file('pic');
        if($file){
            $info = $file->move(ROOT_PATH . 'public' . DS . 'uploads');
            if($info){
                $img_src = '/uploads/'.$info->getSaveName();
                $img_src = str_replace('\\','/',$img_src);
                $data['img'] = $img_src;
            }
        }else{
            $old = $this->getOneById($id);
            $data['img'] = $old['img'];
        }
        $validate = \think\Loader::validate('Goodsmtag');
        if(!$validate->scene('edit')->check($data)){
            return $validate->getError();
        }else{
            $res = db("goodsmtag")->where("id=$id")->update($data);
            if($res)
                return true;
            else
                return false;
        }
    }
Copy after login

The above is the detailed content of How to solve the problem of unique verification when editing in thinkphp5?. For more information, please follow other related articles on the PHP Chinese website!

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