【问题标题】:Laravel custom serviceprovider for validator验证器的 Laravel 自定义服务提供者
【发布时间】:2014-10-22 01:50:23
【问题描述】:

我想覆盖 Illuminate\Validation\Validator 类中的一个方法 (isValidatable)。我通过创建一个扩展 Validator 并且仅覆盖 isValidatable 方法的类(在 Illuminate 之外)来做到这一点。

我认为这会起作用,但我不确定如何为 Validator 类(或者实际上是 CustomLaravelValidator 类)创建服务提供者。我之前创建过服务提供者,但在 Validator 服务提供者 (Illuminate\Validation\ValidationServiceProvider) 内部似乎发生了很多事情。因此,我不知道此类的自定义服务提供者应该是什么样子。

这是我的 CustomLaravelValidator 类:

<?php namespace API\Extensions\Core;

use Illuminate\Validation\Validator;

class CustomLaravelValidator extends Validator {

    /**
     * Determine if the attribute is validatable.
     *
     * @param  string  $rule
     * @param  string  $attribute
     * @param  mixed   $value
     * @return bool
     */
    protected function isValidatable($rule, $attribute, $value)
    {
        // Validate integers on empty strings as well
        if($rule == 'IntStrict')
        {
            return true;
        }
        return $this->presentOrRuleIsImplicit($rule, $attribute, $value) &&
        $this->passesOptionalCheck($attribute);
    }

}

这是来自 Laravel 的默认 ValidationServiceProvider:

<?php namespace Illuminate\Validation;

use Illuminate\Support\ServiceProvider;

class ValidationServiceProvider extends ServiceProvider {

    /**
     * Indicates if loading of the provider is deferred.
     *
     * @var bool
     */
    protected $defer = true;

    /**
     * Register the service provider.
     *
     * @return void
     */
    public function register()
    {
        $this->registerPresenceVerifier();

        $this->app->bindShared('validator', function($app)
        {
            $validator = new Factory($app['translator'], $app);

            // The validation presence verifier is responsible for determining the existence
            // of values in a given data collection, typically a relational database or
            // other persistent data stores. And it is used to check for uniqueness.
            if (isset($app['validation.presence']))
            {
                $validator->setPresenceVerifier($app['validation.presence']);
            }

            return $validator;
        });
    }

    /**
     * Register the database presence verifier.
     *
     * @return void
     */
    protected function registerPresenceVerifier()
    {
        $this->app->bindShared('validation.presence', function($app)
        {
            return new DatabasePresenceVerifier($app['db']);
        });
    }

    /**
     * Get the services provided by the provider.
     *
     * @return array
     */
    public function provides()
    {
        return array('validator', 'validation.presence');
    }

}

谁能告诉我我的自定义服务提供者应该是什么样子的?

【问题讨论】:

    标签: php validation laravel laravel-4 service-provider


    【解决方案1】:

    您的服务提供者不需要模仿本地验证器服务提供者。您只需要在验证器工厂上使用resolver 方法注册您的自定义验证器。

    use API\Extensions\Core\CustomLaravelValidator;
    
    class CustomValidationServiceProvider extends ServiceProvider {
    
        public function boot()
        {
            $this->app['validator']
                 ->resolver(function($translator, $data, $rules, $messages)
            {
                return new CustomLaravelValidator(
                    $translator, 
                    $data, 
                    $rules, 
                    $messages
                );
            });
        }
    }
    

    就是这样……

    【讨论】:

    猜你喜欢
    • 2014-11-09
    • 2014-12-01
    • 2020-01-19
    • 2015-09-12
    • 2016-01-08
    • 2014-08-18
    • 1970-01-01
    • 2017-01-11
    • 2019-05-29
    相关资源
    最近更新 更多