I am trying to create my own custom laravel custom validation rule using the service provider way.
I came up with the following which is stored inside 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 );
}
}
?>
and my provider stored inside the providers folder
<?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()
{
//
}
}
and I have added the provider to my 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
],
The Provider i added seems to conflict with the Felixkiss Provider
as whenever I try to validate a rule like
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',
];
I come up with the following error which actually is a method on the other provider. I am not sure what is the problem here as I seem to have done everything by the book.
via Chebli Mohamed
Aucun commentaire:
Enregistrer un commentaire