Laravel定制服务提供商用于验证器

时间:2014-08-28 08:59:07

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

我想覆盖isValidatable类中的方法(Illuminate\Validation\Validator)。我通过创建一个扩展Validator的类(在Illuminate之外)并且只覆盖isValidatable方法来完成此操作。

我认为这会有效,除了我不确定如何为Validator类(或实际的CustomLaravelValidator类)创建服务提供程序。我以前创建过服务提供程序,但似乎在Validator serviceprovider(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');
    }

}

有谁能告诉我我的自定义服务提供商必须是什么样的?

1 个答案:

答案 0 :(得分:5)

您的服务提供商不需要模仿本机Validator服务提供商。您只需要在验证器工厂使用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
            );
        });
    }
}

就是这样......