Extending the Validator class in Laravel

*爱你&永不变心* 提交于 2019-12-24 03:39:08

问题


I wanted extend the Validator class in Laravel. However in all examples the make method is used to create a new instance which I can't find in the Validator source code. How can I override this method? The constructor requires a TranslatorInterface instance so that doesn't seem to be an option?


回答1:


The make method is actually in Illuminate\Validation\Factory.

If you want to extend this method then you'll need to swap out the IoC binding. Just overload the binding in the container.

App::bindShared('validator', function($app)
{
    $validator = new \Your\Validator\Factory($app['translator'], $app);

    if (isset($app['validation.presence']))
    {
        $validator->setPresenceVerifier($app['validation.presence']);
    }

    return $validator;
});


来源:https://stackoverflow.com/questions/22730751/extending-the-validator-class-in-laravel

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!