Skip to content

Commit 25fd6e7

Browse files
authored
Merge pull request #97 from milwad-dev/feat/register-rules-in-container
[1.x] Register rules in container
2 parents e5c02bd + 32fd4b8 commit 25fd6e7

File tree

3 files changed

+28
-2
lines changed

3 files changed

+28
-2
lines changed

config/laravel-validate.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,4 +44,9 @@
4444
'TR' => TRPhoneValidator::class, // Turkey
4545
'ZH' => ZHPhoneValidator::class, // China
4646
],
47+
48+
/*
49+
* If you want to use rules like 'required|ValidPhone' in your validations, you can change it to true.
50+
*/
51+
'using_container' => false,
4752
];

src/LaravelValidateServiceProvider.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@
33
namespace Milwad\LaravelValidate;
44

55
use Illuminate\Support\Facades\File;
6+
use Illuminate\Support\Facades\Validator;
67
use Illuminate\Support\ServiceProvider;
8+
use Illuminate\Support\Str;
79
use Milwad\LaravelValidate\Utils\CountryPhoneCallback;
810

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

3234
foreach ($langs as $lang) {
35+
$lang = Str::after($lang, 'lang');
36+
$lang = Str::replace('\\', '', $lang);
37+
$lang = Str::replace('/', '', $lang);
38+
3339
$this->publishes([
3440
__DIR__."/lang/$lang" => lang_path($lang),
3541
], "validate-lang-$lang");
@@ -58,5 +64,21 @@ public function boot(): void
5864
foreach ($countries as $code => $country) {
5965
CountryPhoneCallback::addValidator($code, $country);
6066
}
67+
68+
// Register rules in container
69+
if (config('laravel-validate.using_container', false)) {
70+
$rules = File::files(__DIR__.'/Rules');
71+
72+
foreach ($rules as $rule) {
73+
$className = 'Milwad\\LaravelValidate\\Rules\\'.$rule->getBasename('.php');
74+
75+
Validator::extend(
76+
$rule->getFilenameWithoutExtension(),
77+
function ($attribute, $value, $parameters, $validator) use ($className) {
78+
return (new $className($parameters))->passes($attribute, $value);
79+
}
80+
);
81+
}
82+
}
6183
}
6284
}

tests/LaravelValidateServiceProviderTest.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ class LaravelValidateServiceProviderTest extends TestCase
1212
*/
1313
public function test_all_lang_folders_publish_successfully()
1414
{
15-
$langs = File::directories(__DIR__.'/../src/lang');
15+
$langs = File::directories(realpath(__DIR__.'/../src/lang'));
1616

1717
foreach ($langs as $lang) {
1818
$lang = Str::after($lang, 'lang');
@@ -32,7 +32,6 @@ public function test_all_lang_folders_publish_successfully()
3232
*/
3333
public function test_config_file_publish_successful()
3434
{
35-
$this->withoutExceptionHandling();
3635
$this->artisan('vendor:publish', [
3736
'--tag' => 'laravel-validate-config',
3837
]);

0 commit comments

Comments
 (0)