|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Backpack\CRUD\app\Library\Validation\Rules; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Contracts\Validation\Rule; |
|
6
|
|
|
use Illuminate\Contracts\Validation\ValidationRule; |
|
7
|
|
|
use Illuminate\Support\Arr; |
|
8
|
|
|
use Illuminate\Support\Facades\Validator; |
|
9
|
|
|
use Illuminate\Support\Str; |
|
10
|
|
|
|
|
11
|
|
|
abstract class ValidFileArray extends BackpackCustomRule |
|
12
|
|
|
{ |
|
13
|
|
|
public static function field(string|array|ValidationRule|Rule $rules = []): self |
|
14
|
|
|
{ |
|
15
|
|
|
$instance = new static(); |
|
16
|
|
|
$instance->fieldRules = self::getRulesAsArray($rules); |
|
17
|
|
|
|
|
18
|
|
|
if (! in_array('array', $instance->getFieldRules())) { |
|
19
|
|
|
$instance->fieldRules[] = 'array'; |
|
20
|
|
|
} |
|
21
|
|
|
|
|
22
|
|
|
return $instance; |
|
23
|
|
|
} |
|
24
|
|
|
|
|
25
|
|
|
protected function validateFileRules(string $attribute, mixed $items): array |
|
26
|
|
|
{ |
|
27
|
|
|
$cleanAttribute = Str::afterLast($attribute, '.'); |
|
28
|
|
|
$errors = []; |
|
29
|
|
|
|
|
30
|
|
|
// we validate each file individually to avoid returning messages like: `field.0` is not a pdf. |
|
31
|
|
|
foreach ($items as $file) { |
|
32
|
|
|
if (is_file($file)) { |
|
33
|
|
|
$validator = Validator::make( |
|
34
|
|
|
[ |
|
35
|
|
|
$cleanAttribute => $file, |
|
36
|
|
|
], |
|
37
|
|
|
[ |
|
38
|
|
|
$cleanAttribute => $this->getFileRules(), |
|
39
|
|
|
], |
|
40
|
|
|
$this->validator->customMessages, |
|
|
|
|
|
|
41
|
|
|
$this->validator->customAttributes |
|
|
|
|
|
|
42
|
|
|
); |
|
43
|
|
|
|
|
44
|
|
|
if ($validator->fails()) { |
|
45
|
|
|
foreach ($validator->errors()->messages() ?? [] as $attr => $message) { |
|
46
|
|
|
foreach ($message as $messageText) { |
|
47
|
|
|
$errors[] = $messageText; |
|
48
|
|
|
} |
|
49
|
|
|
} |
|
50
|
|
|
} |
|
51
|
|
|
} |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
return $errors; |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
protected function ensureValueIsValid($value) |
|
58
|
|
|
{ |
|
59
|
|
|
if (! is_array($value)) { |
|
60
|
|
|
try { |
|
61
|
|
|
$value = json_decode($value, true) ?? []; |
|
62
|
|
|
} catch(\Exception $e) { |
|
63
|
|
|
return false; |
|
64
|
|
|
} |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
return $value; |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
protected function prepareValidatorData(array $data, $attribute): array |
|
71
|
|
|
{ |
|
72
|
|
|
return Arr::has($data, $attribute) ? [$attribute => Arr::get($data, $attribute)] : $data; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
/* public function validateFieldRules(string $attribute, mixed $value = null, array|null $data = null, array|null $customRules = null): array |
|
76
|
|
|
{ |
|
77
|
|
|
|
|
78
|
|
|
$validatorData = $this->prepareValidatorData($data, $attribute); |
|
79
|
|
|
|
|
80
|
|
|
$validator = Validator::make($validatorData, [ |
|
81
|
|
|
$attribute => $rules, |
|
82
|
|
|
], $this->validator->customMessages, $this->validator->customAttributes); |
|
83
|
|
|
|
|
84
|
|
|
$errors = []; |
|
85
|
|
|
if ($validator->fails()) { |
|
86
|
|
|
foreach ($validator->errors()->messages()[$attribute] as $message) { |
|
87
|
|
|
$errors[] = $message; |
|
88
|
|
|
} |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
return $errors; |
|
92
|
|
|
} */ |
|
93
|
|
|
|
|
94
|
|
|
/** |
|
95
|
|
|
* Run both field and file validations. |
|
96
|
|
|
*/ |
|
97
|
|
|
protected function validateFieldAndFile(string $attribute, mixed $value = null, ?array $data = null, array|null $customRules = null): array |
|
98
|
|
|
{ |
|
99
|
|
|
$fieldErrors = $this->validateFieldRules($attribute, $value, $data, $customRules); |
|
|
|
|
|
|
100
|
|
|
$fileErrors = $this->validateFileRules($attribute, $value); |
|
101
|
|
|
|
|
102
|
|
|
return array_merge($fieldErrors, $fileErrors); |
|
103
|
|
|
} |
|
104
|
|
|
} |
|
105
|
|
|
|