|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Backpack\CRUD\app\Library\Uploaders\Validation; |
|
4
|
|
|
|
|
5
|
|
|
use Backpack\CRUD\app\Library\CrudPanel\CrudPanelFacade; |
|
6
|
|
|
use Closure; |
|
7
|
|
|
use Illuminate\Support\Facades\Validator; |
|
8
|
|
|
|
|
9
|
|
|
class ValidUploadMultiple extends ValidBackpackUpload |
|
10
|
|
|
{ |
|
11
|
|
|
/** |
|
12
|
|
|
* Run the validation rule. |
|
13
|
|
|
* |
|
14
|
|
|
* @param string $attribute |
|
15
|
|
|
* @param mixed $value |
|
16
|
|
|
* @param \Closure(string): \Illuminate\Translation\PotentiallyTranslatedString $fail |
|
17
|
|
|
* @return void |
|
18
|
|
|
*/ |
|
19
|
|
|
public function validate(string $attribute, mixed $value, Closure $fail): void |
|
20
|
|
|
{ |
|
21
|
|
|
if (! is_array($value)) { |
|
22
|
|
|
try { |
|
23
|
|
|
$value = json_decode($value, true); |
|
24
|
|
|
} catch(\Exception $e) { |
|
25
|
|
|
$fail('Unable to determine the value type'); |
|
26
|
|
|
|
|
27
|
|
|
return; |
|
28
|
|
|
} |
|
29
|
|
|
} |
|
30
|
|
|
// `upload_multiple` sends [[0 => null]] (null after `ConvertEmptyStringsToNull`) when nothing changes on the field |
|
31
|
|
|
// for that reason we need to manually check what we are getting from the request |
|
32
|
|
|
if (CrudPanelFacade::getCurrentEntry() !== false) { |
|
|
|
|
|
|
33
|
|
|
$filesToClear = CrudPanelFacade::getRequest()->input('clear_'.$attribute) ?? []; |
|
|
|
|
|
|
34
|
|
|
$previousFiles = CrudPanelFacade::getCurrentEntry()->{$attribute} ?? []; |
|
35
|
|
|
|
|
36
|
|
|
if (is_string($previousFiles) && ! isset(CrudPanelFacade::getCurrentEntry()->getCasts()[$attribute])) { |
|
37
|
|
|
$previousFiles = json_decode($previousFiles, true); |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
$previousFilesWithoutCleared[$attribute] = array_diff($previousFiles, $filesToClear); |
|
|
|
|
|
|
41
|
|
|
|
|
42
|
|
|
// we are only going to check if the deleted files could break the validation rules |
|
43
|
|
|
if (count($value) === 1 && empty($value[0])) { |
|
44
|
|
|
$validator = Validator::make($previousFilesWithoutCleared, [ |
|
45
|
|
|
$attribute => $this->arrayRules, |
|
46
|
|
|
], $this->validator->customMessages, $this->validator->customAttributes); |
|
|
|
|
|
|
47
|
|
|
|
|
48
|
|
|
if ($validator->fails()) { |
|
49
|
|
|
$fail($validator->errors()->first($attribute)); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
return; |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
// we are now going to check if the previous files - deleted files + new files still pass the validation |
|
56
|
|
|
$previousFilesWithoutClearedPlusNewFiles[$attribute] = array_merge($previousFilesWithoutCleared[$attribute], $value); |
|
|
|
|
|
|
57
|
|
|
|
|
58
|
|
|
$validator = Validator::make($previousFilesWithoutClearedPlusNewFiles, [ |
|
59
|
|
|
$attribute => $this->arrayRules, |
|
60
|
|
|
], $this->validator->customMessages, $this->validator->customAttributes); |
|
61
|
|
|
|
|
62
|
|
|
if ($validator->fails()) { |
|
63
|
|
|
$fail($validator->errors()->first($attribute)); |
|
64
|
|
|
|
|
65
|
|
|
return; |
|
66
|
|
|
} |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
// we are now going to perform the file validation on the actual files |
|
70
|
|
|
foreach ($value as $file) { |
|
71
|
|
|
$validator = Validator::make([$attribute => $file], [ |
|
72
|
|
|
$attribute => $this->fileRules, |
|
73
|
|
|
], $this->validator->customMessages, $this->validator->customAttributes); |
|
74
|
|
|
|
|
75
|
|
|
if ($validator->fails()) { |
|
76
|
|
|
$fail($validator->errors()->first($attribute)); |
|
77
|
|
|
} |
|
78
|
|
|
} |
|
79
|
|
|
} |
|
80
|
|
|
} |
|
81
|
|
|
|