我想覆盖类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');
}
}
谁能告诉我我的自定义服务提供商的外观?