Support to rewrite the rules for scene when using validation. by limit81995 · Pull Request #7469 · hyperf/hyperf

例如使用validation功能的以下场景时:

    protected array $scenes = [
        "detail" => [
            "phone" => ["regex:/^1[345789]\d{9}$/"],
            "username",
            "email",
        ],
    ];


    public function rules(): array
    {
        return [
            "phone" => ["required", "regex:/^1[345789]\d{9}$/"],
        ];
    }

之前版本是会报错。如果删除$scenes["detail"]["phone"]的赋值后,会正常使用,但是场景只能引用下面定义的phone规则。

有时会遇到,A接口和B接口都需要用phone,但是A接口phone是必填,B接口phone是非必填,但是如果填写的话就需要走phone的规则。 如果用其他方式实现会比较复杂,例如创建一个新的验证器,那么message和attributes的方法都要复制过去。再或者写很多方法去进行手动验证,这样极不方便解耦和提高效率。

所以如果在scenes中可以直接将场景的使用的参数进行覆盖就可以大大减少很多工作量。