2

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

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

这是我的 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');
    }

}

谁能告诉我我的自定义服务提供商的外观?

4

1 回答 1

6

您的服务提供者不需要模仿原生验证器服务提供者。您只需要使用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-08-28T14:32:00.873 回答