| 1 | <?php |
||||
| 2 | |||||
| 3 | declare(strict_types=1); |
||||
| 4 | |||||
| 5 | namespace Laravel\ExtraFieldsValidator; |
||||
| 6 | |||||
| 7 | trait ProvidesExtraFieldsValidator |
||||
| 8 | { |
||||
| 9 | public function validator() |
||||
| 10 | { |
||||
| 11 | return $this->makeExtraFieldsValidator(); |
||||
| 12 | } |
||||
| 13 | |||||
| 14 | protected function makeExtraFieldsValidator() |
||||
| 15 | { |
||||
| 16 | /** @var ValidationFactory $factory */ |
||||
| 17 | $factory = $this->container->make(ValidationFactory::class); |
||||
| 18 | $rules = method_exists($this, 'rules') ? $this->container->call([$this, 'rules']) : []; |
||||
| 19 | $data = $this->getExtraFieldsValidationData(); |
||||
| 20 | |||||
| 21 | /** @var Validator $validator */ |
||||
| 22 | $validator = $factory->make($data, $rules, $this->messages(), $this->attributes()); |
||||
| 23 | |||||
| 24 | return $validator->afterSuccess(function (Validator $validator) { |
||||
| 25 | /** @var ExtraFieldsDetector $detector */ |
||||
| 26 | $detector = $this->container->make( |
||||
| 27 | config('extra-validator.extra_fields_detector', ExtraFieldsDetector::class) |
||||
| 28 | ); |
||||
| 29 | |||||
| 30 | $extraField = $detector->getFirstExtraField($validator->getData(), $validator->getRules()); |
||||
| 31 | |||||
| 32 | if (is_null($extraField)) { |
||||
| 33 | return true; |
||||
| 34 | } |
||||
| 35 | |||||
| 36 | $validator->errors()->add($extraField, $this->getExtraFieldErrorMessage($extraField)); |
||||
| 37 | |||||
| 38 | return false; |
||||
| 39 | }); |
||||
| 40 | } |
||||
| 41 | |||||
| 42 | protected function getExtraFieldsValidationData() |
||||
| 43 | { |
||||
| 44 | switch (config('extra-validator.data_source', ExtraValidatorDataSource::DEFAULT)) { |
||||
| 45 | case ExtraValidatorDataSource::INPUT_SOURCE: |
||||
| 46 | return $this->getInputSource()->all(); |
||||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||||
| 47 | default: |
||||
| 48 | return $this->validationData(); |
||||
|
0 ignored issues
–
show
The method
validationData() does not exist on Laravel\ExtraFieldsValid...desExtraFieldsValidator. Did you maybe mean validator()?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces. This is most likely a typographical error or the method has been renamed. Loading history...
|
|||||
| 49 | } |
||||
| 50 | } |
||||
| 51 | |||||
| 52 | public function getExtraFieldErrorMessage(string $field): string |
||||
| 53 | { |
||||
| 54 | return trans('validation.custom.extra_field', ['attribute' => $field]); |
||||
| 55 | } |
||||
| 56 | } |
||||
| 57 |