我正在尝试使用服务提供商方式创建自己的自定义laravel自定义验证规则。
我想出了以下内容,它存储在app/Services/CustomValidator.php
<?php
namespace App\Services;
class CustomValidator extends \Illuminate\Validation\Validator {
public function __construct($translator, $data, $rules, $messages = array(), $customAttributes = array())
{
parent::__construct($translator, $data, $rules, $messages, $customAttributes);
$this->setCustomMessages([
'not_empty_string'=> 'The field must not be empty'
]);
}
public function validateNotEmptyString($attribute, $value, $parameters)
{
return (bool) preg_match( '/""/', $value );
}
}
?>
我的提供者存储在providers文件夹
中<?php
namespace App\Providers;
use Validator;
use Illuminate\Support\ServiceProvider;
use App\Services\CustomValidator;
class CustomValidationServiceProvider extends ServiceProvider
{
/**
* Bootstrap the application services.
*
* @return void
*/
public function boot()
{
Validator::resolver(function($translator, $data, $rules, $messages)
{
return new CustomValidator($translator, $data, $rules, $messages);
});
}
/**
* Register the application services.
*
* @return void
*/
public function register()
{
//
}
}
我已将提供程序添加到我的config / app.php
'providers' => [
/*
* Laravel Framework Service Providers...
*/
Illuminate\Auth\AuthServiceProvider::class,
Illuminate\Broadcasting\BroadcastServiceProvider::class,
Illuminate\Bus\BusServiceProvider::class,
Illuminate\Cache\CacheServiceProvider::class,
Illuminate\Foundation\Providers\ConsoleSupportServiceProvider::class,
Illuminate\Cookie\CookieServiceProvider::class,
Illuminate\Database\DatabaseServiceProvider::class,
Illuminate\Encryption\EncryptionServiceProvider::class,
Illuminate\Filesystem\FilesystemServiceProvider::class,
Illuminate\Foundation\Providers\FoundationServiceProvider::class,
Illuminate\Hashing\HashServiceProvider::class,
Illuminate\Mail\MailServiceProvider::class,
Illuminate\Pagination\PaginationServiceProvider::class,
Illuminate\Pipeline\PipelineServiceProvider::class,
Illuminate\Queue\QueueServiceProvider::class,
Illuminate\Redis\RedisServiceProvider::class,
Illuminate\Auth\Passwords\PasswordResetServiceProvider::class,
Illuminate\Session\SessionServiceProvider::class,
Illuminate\Translation\TranslationServiceProvider::class,
Illuminate\Validation\ValidationServiceProvider::class,
Illuminate\View\ViewServiceProvider::class,
/*
* Application Service Providers...
*/
App\Providers\AppServiceProvider::class,
App\Providers\AuthServiceProvider::class,
App\Providers\EventServiceProvider::class,
App\Providers\RouteServiceProvider::class,
Laravel\Spark\Providers\SparkServiceProvider::class,
App\Providers\SparkServiceProvider::class,
Laravel\Cashier\CashierServiceProvider::class,
//custom classes
Felixkiss\UniqueWithValidator\UniqueWithValidatorServiceProvider::class,
App\Providers\CustomValidationServiceProvider::class
],
我添加的提供商似乎与Felixkiss Provider
冲突,因为每当我尝试验证像
private $rules = [
'name' => 'required|unique_with:tests,suite_id',
'description' => 'not_empty_string|required',
'expected_result' => 'not_empty_string|required',
'suite_id' => 'required',
'project_id' => 'required',
];
我想出了以下错误,实际上是其他提供商的方法。我不知道这里有什么问题,因为我似乎已经完成了本书的所有工作。
答案 0 :(得分:0)
刚刚浏览了@Zamrony的评论,解决方案是扩展uniquewith-validator,然后像往常一样调用它的业务。
<?php
namespace App\Services;
class CustomValidator extends \Felixkiss\UniqueWithValidator\ValidatorExtension {
public function validateNotEmptyString($attribute, $value, $parameters)
{
$this->setCustomMessages([
'not_empty_string'=> 'The field must not be empty'
]);
return (bool) preg_match( '/""/', $value );
}
}
?>
我不确定这个解决方案有多完美,但它对我有用。