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\Str; |
9
|
|
|
use Illuminate\Support\Facades\Validator; |
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
|
|
|
return; |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
if (! $value = self::ensureValidValue($value)) { |
31
|
|
|
$fail('Unable to determine the value type.'); |
32
|
|
|
|
33
|
|
|
return; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
// `upload_multiple` sends [[0 => null]] when user doesn't upload anything |
37
|
|
|
// assume that nothing changed on field so nothing is sent on the request. |
38
|
|
|
if (count($value) === 1 && empty($value[0])) { |
39
|
|
|
if ($entry) { |
40
|
|
|
unset($this->data[$attribute]); |
41
|
|
|
} else { |
42
|
|
|
$this->data[$attribute] = []; |
43
|
|
|
} |
44
|
|
|
$value = []; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
$previousValues = $entry?->{$attribute} ?? []; |
48
|
|
|
if (is_string($previousValues)) { |
49
|
|
|
$previousValues = json_decode($previousValues, true) ?? []; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
$value = array_merge($previousValues, $value); |
|
|
|
|
53
|
|
|
|
54
|
|
|
if ($entry) { |
55
|
|
|
$filesDeleted = CrudPanelFacade::getRequest()->input('clear_'.$attribute) ?? []; |
|
|
|
|
56
|
|
|
|
57
|
|
|
$data = $this->data; |
58
|
|
|
$data[$attribute] = array_diff($value, $filesDeleted); |
59
|
|
|
|
60
|
|
|
$this->validateArrayData($attribute, $fail, $data); |
61
|
|
|
|
62
|
|
|
$this->validateItems($attribute, $value, $fail); |
63
|
|
|
|
64
|
|
|
return; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
$this->validateArrayData($attribute, $fail); |
68
|
|
|
|
69
|
|
|
$this->validateItems($attribute, $value, $fail); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
protected function validateUploadInSubfield($attribute, $value, Closure $fail, $entry = null) |
73
|
|
|
{ |
74
|
|
|
$mainField = Str::before($attribute, '.'); |
75
|
|
|
$subfield = Str::afterLast($attribute, '.'); |
76
|
|
|
$row = (int)Str::before(Str::after($attribute, '.'), '.'); |
77
|
|
|
|
78
|
|
|
$values[$mainField] = Uploader::mergeFilesAndValuesRecursive($this->data[$mainField], $this->data['_order_'.$mainField] ?? []); |
|
|
|
|
79
|
|
|
|
80
|
|
|
if (! array_key_exists($subfield, $values[$mainField][$row]) && $entry) { |
81
|
|
|
return; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
$this->createValidator($subfield, $this->getFieldRules(), $values[$mainField][$row][$subfield] ?? null, $fail); |
85
|
|
|
|
86
|
|
|
if(!empty($value) && !empty($this->getFileRules())) { |
87
|
|
|
$this->createValidator($subfield, $this->getFileRules(), $values[$mainField][$row][$subfield] ?? null, $fail); |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
protected function createValidator(string $attribute, array $rules, mixed $value, Closure $fail): void |
92
|
|
|
{ |
93
|
|
|
$validator = Validator::make([$attribute => $value], [ |
94
|
|
|
$attribute => $rules, |
95
|
|
|
], $this->validator->customMessages, $this->validator->customAttributes); |
|
|
|
|
96
|
|
|
|
97
|
|
|
if ($validator->fails()) { |
98
|
|
|
foreach ($validator->errors()->messages()[$attribute] as $message) { |
99
|
|
|
$fail($message)->translate(); |
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
} |
104
|
|
|
|