|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Backpack\CRUD\app\Library\Validation\Rules; |
|
4
|
|
|
|
|
5
|
|
|
use Backpack\CRUD\app\Library\CrudPanel\CrudPanelFacade; |
|
6
|
|
|
use Backpack\CRUD\app\Library\Uploaders\Uploader; |
|
7
|
|
|
use Closure; |
|
8
|
|
|
use Illuminate\Support\Facades\Validator; |
|
9
|
|
|
use Illuminate\Support\Str; |
|
10
|
|
|
|
|
11
|
|
|
class ValidUploadMultiple extends ValidFileArray |
|
12
|
|
|
{ |
|
13
|
|
|
/** |
|
14
|
|
|
* Run the validation rule. |
|
15
|
|
|
* |
|
16
|
|
|
* @param string $attribute |
|
17
|
|
|
* @param mixed $value |
|
18
|
|
|
* @param \Closure(string): \Illuminate\Translation\PotentiallyTranslatedString $fail |
|
19
|
|
|
* @return void |
|
20
|
|
|
*/ |
|
21
|
|
|
public function validate(string $attribute, mixed $value, Closure $fail): void |
|
22
|
|
|
{ |
|
23
|
|
|
$entry = CrudPanelFacade::getCurrentEntry() ?: null; |
|
|
|
|
|
|
24
|
|
|
|
|
25
|
|
|
if (Str::contains($attribute, '.')) { |
|
26
|
|
|
$this->validateUploadInSubfield($attribute, $value, $fail, $entry); |
|
27
|
|
|
|
|
28
|
|
|
return; |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
if (! $value = self::ensureValidValue($value)) { |
|
32
|
|
|
$fail('Unable to determine the value type.'); |
|
33
|
|
|
|
|
34
|
|
|
return; |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
// `upload_multiple` sends [[0 => null]] when user doesn't upload anything |
|
38
|
|
|
// assume that nothing changed on field so nothing is sent on the request. |
|
39
|
|
|
if (count($value) === 1 && empty($value[0])) { |
|
40
|
|
|
if ($entry) { |
|
41
|
|
|
unset($this->data[$attribute]); |
|
42
|
|
|
} else { |
|
43
|
|
|
$this->data[$attribute] = []; |
|
44
|
|
|
} |
|
45
|
|
|
$value = []; |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
$previousValues = $entry?->{$attribute} ?? []; |
|
49
|
|
|
if (is_string($previousValues)) { |
|
50
|
|
|
$previousValues = json_decode($previousValues, true) ?? []; |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
$value = array_merge($previousValues, $value); |
|
|
|
|
|
|
54
|
|
|
|
|
55
|
|
|
if ($entry) { |
|
56
|
|
|
$filesDeleted = CrudPanelFacade::getRequest()->input('clear_'.$attribute) ?? []; |
|
|
|
|
|
|
57
|
|
|
|
|
58
|
|
|
$data = $this->data; |
|
59
|
|
|
$data[$attribute] = array_diff($value, $filesDeleted); |
|
60
|
|
|
|
|
61
|
|
|
$this->validateArrayData($attribute, $fail, $data); |
|
62
|
|
|
|
|
63
|
|
|
$this->validateItems($attribute, $value, $fail); |
|
64
|
|
|
|
|
65
|
|
|
return; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
$this->validateArrayData($attribute, $fail); |
|
69
|
|
|
|
|
70
|
|
|
$this->validateItems($attribute, $value, $fail); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
protected function validateUploadInSubfield($attribute, $value, Closure $fail, $entry = null) |
|
74
|
|
|
{ |
|
75
|
|
|
$mainField = Str::before($attribute, '.'); |
|
76
|
|
|
$subfield = Str::afterLast($attribute, '.'); |
|
77
|
|
|
$row = (int) Str::before(Str::after($attribute, '.'), '.'); |
|
78
|
|
|
|
|
79
|
|
|
$values[$mainField] = Uploader::mergeFilesAndValuesRecursive($this->data[$mainField], $this->data['_order_'.$mainField] ?? []); |
|
|
|
|
|
|
80
|
|
|
|
|
81
|
|
|
if (! array_key_exists($subfield, $values[$mainField][$row]) && $entry) { |
|
82
|
|
|
return; |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
$this->createValidator($subfield, $this->getFieldRules(), $values[$mainField][$row][$subfield] ?? null, $fail); |
|
86
|
|
|
|
|
87
|
|
|
if (! empty($value) && ! empty($this->getFileRules())) { |
|
88
|
|
|
$this->createValidator($subfield, $this->getFileRules(), $values[$mainField][$row][$subfield] ?? null, $fail); |
|
89
|
|
|
} |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
protected function createValidator(string $attribute, array $rules, mixed $value, Closure $fail): void |
|
93
|
|
|
{ |
|
94
|
|
|
$validator = Validator::make([$attribute => $value], [ |
|
95
|
|
|
$attribute => $rules, |
|
96
|
|
|
], $this->validator->customMessages, $this->validator->customAttributes); |
|
|
|
|
|
|
97
|
|
|
|
|
98
|
|
|
if ($validator->fails()) { |
|
99
|
|
|
foreach ($validator->errors()->messages()[$attribute] as $message) { |
|
100
|
|
|
$fail($message)->translate(); |
|
101
|
|
|
} |
|
102
|
|
|
} |
|
103
|
|
|
} |
|
104
|
|
|
} |
|
105
|
|
|
|