forked from antonioribeiro/countries-laravel
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix antonioribeiro#27 validation rules are not deferred
- Loading branch information
Showing
3 changed files
with
43 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.'.'); | ||
} | ||
} | ||
} |