Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #27 validation rules are not deferred #28

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.'.');
}
}
}