dotif64826 2014-03-21 21:32
浏览 85
已采纳

多次扩展Laravel验证器

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.

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...?

Thanks

//

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...

  • 写回答

2条回答 默认 最新

  • dougaopu7938 2014-03-22 10:45
    关注

    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;
    }
    

    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.

    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';
        }
    
    }
    

    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.

    Hope this helps, it did for me!

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法
  • ¥15 可否在不同线程中调用封装数据库操作的类
  • ¥15 微带串馈天线阵列每个阵元宽度计算
  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据