|
4 | 4 |
|
5 | 5 | namespace Longman\LaravelLodash; |
6 | 6 |
|
| 7 | +use Illuminate\Contracts\Translation\Translator; |
7 | 8 | use Illuminate\Http\Request; |
8 | | -use Illuminate\Support\Facades\Validator; |
9 | 9 | use Illuminate\Support\ServiceProvider as LaravelServiceProvider; |
10 | 10 | use InvalidArgumentException; |
11 | 11 | use Longman\LaravelLodash\Commands\ClearAll; |
|
16 | 16 | use Longman\LaravelLodash\Commands\UserAdd; |
17 | 17 | use Longman\LaravelLodash\Commands\UserPassword; |
18 | 18 | use Longman\LaravelLodash\Validation\StrictTypeValidator; |
| 19 | +use Longman\LaravelLodash\Validation\Validator; |
19 | 20 |
|
20 | 21 | use function app; |
21 | 22 | use function array_keys; |
@@ -45,11 +46,39 @@ public function boot(): void |
45 | 46 | __DIR__ . '/../config/config.php' => config_path('lodash.php'), |
46 | 47 | ]); |
47 | 48 |
|
| 49 | + // Validator and rules |
| 50 | + $this->app['validator'] |
| 51 | + ->resolver( |
| 52 | + function (Translator $translator, array $data, array $rules, array $messages): Validator { |
| 53 | + $validator = new Validator( |
| 54 | + $translator, |
| 55 | + $data, |
| 56 | + $rules, |
| 57 | + $messages, |
| 58 | + ); |
| 59 | + |
| 60 | + $validator->addExtension('strict', function (string $attribute, mixed $value, array $parameters, Validator $validator): bool { |
| 61 | + if (empty($parameters[0])) { |
| 62 | + throw new InvalidArgumentException('Strict rule requires an type argument'); |
| 63 | + } |
| 64 | + |
| 65 | + $validator->addReplacer('strict', static function (string $message, string $attribute, string $rule, array $parameters): string { |
| 66 | + return str_replace(':type', $parameters[0], $message); |
| 67 | + }); |
| 68 | + |
| 69 | + $customValidator = $this->app->make(StrictTypeValidator::class); |
| 70 | + |
| 71 | + return $customValidator->validate($attribute, $value, $parameters); |
| 72 | + }); |
| 73 | + |
| 74 | + return $validator; |
| 75 | + }, |
| 76 | + ); |
| 77 | + |
48 | 78 | $this->registerBladeDirectives(); |
49 | 79 |
|
50 | 80 | $this->loadTranslations(); |
51 | | - |
52 | | - $this->loadValidations(); |
| 81 | + //$this->loadValidations(); |
53 | 82 | } |
54 | 83 |
|
55 | 84 | public function register(): void |
@@ -131,25 +160,4 @@ protected function loadTranslations(): void |
131 | 160 | __DIR__ . '/../translations' => resource_path('lang/vendor/lodash'), |
132 | 161 | ]); |
133 | 162 | } |
134 | | - |
135 | | - protected function loadValidations(): void |
136 | | - { |
137 | | - if (! config('lodash.register.validation_rules')) { |
138 | | - return; |
139 | | - } |
140 | | - |
141 | | - Validator::extend('strict', function (string $attribute, mixed $value, array $parameters, Validator $validator): bool { |
142 | | - if (empty($parameters[0])) { |
143 | | - throw new InvalidArgumentException('Strict rule requires an type argument'); |
144 | | - } |
145 | | - |
146 | | - $validator->addReplacer('strict', static function (string $message, string $attribute, string $rule, array $parameters): string { |
147 | | - return str_replace(':type', $parameters[0], $message); |
148 | | - }); |
149 | | - |
150 | | - $customValidator = $this->app->make(StrictTypeValidator::class); |
151 | | - |
152 | | - return $customValidator->validate($attribute, $value, $parameters); |
153 | | - }, 'The :attribute must be of type :type'); |
154 | | - } |
155 | 163 | } |
0 commit comments