Skip to content

Commit

Permalink
Fix antonioribeiro#27 validation rules are not deferred
Browse files Browse the repository at this point in the history
  • Loading branch information
ametad committed Jul 12, 2021
1 parent 0d83b2f commit c8afaf0
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 22 deletions.
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,8 @@
"extra": {
"laravel": {
"providers": [
"PragmaRX\\CountriesLaravel\\Package\\ServiceProvider"
"PragmaRX\\CountriesLaravel\\Package\\ServiceProvider",
"PragmaRX\\CountriesLaravel\\Package\\ValidationServiceProvider"
],
"aliases": {
"Countries": "PragmaRX\\CountriesLaravel\\Package\\Facade"
Expand Down
22 changes: 1 addition & 21 deletions src/package/ServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
namespace PragmaRX\CountriesLaravel\Package;

use Illuminate\Support\Facades\Route;
use Illuminate\Support\Facades\Validator;
use Illuminate\Support\ServiceProvider as IlluminateServiceProvider;
use PragmaRX\Countries\Package\Countries as CountriesService;
use PragmaRX\Countries\Package\Data\Repository;
Expand All @@ -12,7 +11,6 @@
use PragmaRX\Countries\Package\Services\Helper;
use PragmaRX\Countries\Package\Services\Hydrator;
use PragmaRX\CountriesLaravel\Package\Console\Commands\Update;
use PragmaRX\CountriesLaravel\Package\Facade as CountriesFacade;

class ServiceProvider extends IlluminateServiceProvider
{
Expand Down Expand Up @@ -62,9 +60,7 @@ protected function mergeConfig()
*/
public function boot()
{
if (config('countries.validation.enabled')) {
$this->addValidators();
}
//
}

/**
Expand Down Expand Up @@ -129,22 +125,6 @@ protected function registerService()
});
}

/**
* Add validators.
*/
protected function addValidators()
{
foreach (config('countries.validation.rules') as $ruleName => $countryAttribute) {
if (is_int($ruleName)) {
$ruleName = $countryAttribute;
}

Validator::extend($ruleName, function ($attribute, $value) use ($countryAttribute) {
return ! CountriesFacade::where($countryAttribute, $value)->isEmpty();
}, 'The :attribute must be a valid '.$ruleName.'.');
}
}

/**
* Register update command.
*/
Expand Down
40 changes: 40 additions & 0 deletions src/package/ValidationServiceProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

namespace PragmaRX\CountriesLaravel\Package;

use Illuminate\Support\Facades\Validator;
use Illuminate\Support\ServiceProvider as IlluminateServiceProvider;
use PragmaRX\CountriesLaravel\Package\Facade as CountriesFacade;

class ValidationServiceProvider extends IlluminateServiceProvider
{
protected $defer = false;

/**
* Bootstrap any application services.
*
* @return void
*/
public function boot()
{
if (config('countries.validation.enabled')) {
$this->addValidators();
}
}

/**
* Add validators.
*/
protected function addValidators()
{
foreach (config('countries.validation.rules') as $ruleName => $countryAttribute) {
if (is_int($ruleName)) {
$ruleName = $countryAttribute;
}

Validator::extend($ruleName, function ($attribute, $value) use ($countryAttribute) {
return ! CountriesFacade::where($countryAttribute, $value)->isEmpty();
}, 'The :attribute must be a valid '.$ruleName.'.');
}
}
}

0 comments on commit c8afaf0

Please sign in to comment.