多次扩展Laravel验证器 [英] Extend Laravel validator multiple times

查看:142
本文介绍了多次扩展Laravel验证器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

类似于confirmation规则,我正在基于另一个属性的值创建一个验证规则.根据我的发现,完成此操作的唯一方法是扩展Validator类并通过$this->data获取值.

Similar to the confirmation rule, I'm creating a validation rule that's based on the value of another attribute. From what I've found the only way to accomplish this is by extending the Validator class and get the value through $this->data.

文档说要扩展Validator,然后使用Validator::resolver注册新的扩展类.这很好用,但是仅在使用单个解析器的情况下,因为似乎每个后续定义的解析器都将覆盖先前的解析器.

The docs say to extend the Validator and then use Validator::resolver to register the new extension class. This works fine, but only in the case of a single resolver as it seems each subsequent defined resolver simply overrides the previous one.

该问题如何解决?我们如何定义自定义验证器,这些自定义验证器仍可以访问其他属性,而将所有方法都放在同一个类中?

How can this problem be solved? How can we define custom validators that still have access to other attributes without putting all methods in the same class...?

谢谢

//

注意:我之所以这样问是因为我想发布一些验证程序包,但是按照上述推理,如果有人安装多个程序包,它们将变得毫无用处...

Note: I'm asking this because I'd like to release a few validator packages, but following the reasoning above they'd just become useless if someone installs multiple packages...

推荐答案

是的,它被覆盖的原因是Validator\Factory类仅存储一个resolver.该函数的代码在这里:

Yup, the reason it is getting overridden is the Validator\Factory class only stores one resolver. The code for the function is here:

public function resolver(Closure $resolver) {
    $this->resolver = $resolver;
}

我认为resolver方法的重点是用您自己的方法扩展基本验证类.这很有意义,因为您可以实例化UserRegistrationValidator来添加自己的验证规则,该验证规则具有与现有规则一样的灵活性.

I would assume that the point of the resolver method is to extend the base validation class with your own. This makes sense as you could instantiate a UserRegistrationValidator adding your own validation rules that have all the flexibility as the existing ones.

与此有关的一个问题是可以轻松地覆盖它,这向我表明您仅应在调用make之前调用resolver方法.尽管付出了更多的努力,但它会阻止来自不同程序包的规则可能自动覆盖其他规则,甚至自动覆盖基本规则.

One issue with this is that it can be overridden with ease, this indicates to me that you should only call the resolver method just before calling make. Although more effort, it would stop rules from different packages potentially overriding other rules and even the base ones automatically.

但是,这对于仅提供额外有用规则的软件包来说效果不佳.添加规则的更简单版本是:

But this does not work well for a package that only provides extra helpful rules. The simpler version to add a rule is:

Validator::extend('foo', 'FooValidator@validate');

这不允许访问输入数据,这对于复杂的规则很重要. 文档示例还向我们显示了这一点:

This does not allow access to the input data, which is important for complex rules. The documentation example shows us this also:

class CustomValidator extends Illuminate\Validation\Validator 
{

    public function validateFoo($attribute, $value, $parameters) {
        return $value == 'foo';
    }

}

但是等等!文档没有告诉您的是,您可以添加另一个参数来获取Validator的实例.在撰写此答案并深入学习课堂时,我才发现自己的问题!

But wait! What the documentation doesn't tell you is that you can add on another parameter to get the instance of Validator. Which I just found out myself whilst writing this answer and getting deep into the classes!

class TestRulesValidator
{

    public function validateTestRule($attribute, $value, $params, $validator) {
        var_dump($validator->getData());
    }

}


Validator::extend('test_rule', 'TestRulesValidator@validateTestRule');

因此,可以得出结论,传递一个额外的参数,该参数将成为所使用的验证程序的实例.我怀疑这也可以与回调一起使用.

So to conclude, pass an extra parameter which will be the instance of the validator being used. I suspect this will also work with a callback too.

希望这对我有帮助!

这篇关于多次扩展Laravel验证器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆