Skip to content
Merged
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
5 changes: 5 additions & 0 deletions config/laravel-validate.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,9 @@
'TR' => TRPhoneValidator::class, // Turkey
'ZH' => ZHPhoneValidator::class, // China
],

/*
* If you want to use rules like 'required|ValidPhone' in your validations, you can change it to true.
*/
'using_container' => false,
];
22 changes: 22 additions & 0 deletions src/LaravelValidateServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
namespace Milwad\LaravelValidate;

use Illuminate\Support\Facades\File;
use Illuminate\Support\Facades\Validator;
use Illuminate\Support\ServiceProvider;
use Illuminate\Support\Str;
use Milwad\LaravelValidate\Utils\CountryPhoneCallback;

class LaravelValidateServiceProvider extends ServiceProvider
Expand All @@ -30,6 +32,10 @@ protected function publishLangFiles(): void
$langs = File::directories(__DIR__.'/lang');

foreach ($langs as $lang) {
$lang = Str::after($lang, 'lang');
$lang = Str::replace('\\', '', $lang);
$lang = Str::replace('/', '', $lang);

$this->publishes([
__DIR__."/lang/$lang" => lang_path($lang),
], "validate-lang-$lang");
Expand Down Expand Up @@ -58,5 +64,21 @@ public function boot(): void
foreach ($countries as $code => $country) {
CountryPhoneCallback::addValidator($code, $country);
}

// Register rules in container
if (config('laravel-validate.using_container', false)) {
$rules = File::files(__DIR__.'/Rules');

foreach ($rules as $rule) {
$className = 'Milwad\\LaravelValidate\\Rules\\'.$rule->getBasename('.php');

Validator::extend(
$rule->getFilenameWithoutExtension(),
function ($attribute, $value, $parameters, $validator) use ($className) {
return (new $className($parameters))->passes($attribute, $value);
}
);
}
}
}
}
3 changes: 1 addition & 2 deletions tests/LaravelValidateServiceProviderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class LaravelValidateServiceProviderTest extends TestCase
*/
public function test_all_lang_folders_publish_successfully()
{
$langs = File::directories(__DIR__.'/../src/lang');
$langs = File::directories(realpath(__DIR__.'/../src/lang'));

foreach ($langs as $lang) {
$lang = Str::after($lang, 'lang');
Expand All @@ -32,7 +32,6 @@ public function test_all_lang_folders_publish_successfully()
*/
public function test_config_file_publish_successful()
{
$this->withoutExceptionHandling();
$this->artisan('vendor:publish', [
'--tag' => 'laravel-validate-config',
]);
Expand Down