|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Backpack\CRUD\app\Library\Uploaders\Validation; |
|
4
|
|
|
|
|
5
|
|
|
use Backpack\CRUD\app\Library\CrudPanel\CrudPanelFacade; |
|
6
|
|
|
use Closure; |
|
7
|
|
|
|
|
8
|
|
|
class ValidUploadMultiple extends ValidArray |
|
9
|
|
|
{ |
|
10
|
|
|
/** |
|
11
|
|
|
* Run the validation rule. |
|
12
|
|
|
* |
|
13
|
|
|
* @param string $attribute |
|
14
|
|
|
* @param mixed $value |
|
15
|
|
|
* @param \Closure(string): \Illuminate\Translation\PotentiallyTranslatedString $fail |
|
16
|
|
|
* @return void |
|
17
|
|
|
*/ |
|
18
|
|
|
public function validate(string $attribute, mixed $value, Closure $fail): void |
|
19
|
|
|
{ |
|
20
|
|
|
if (! is_array($value)) { |
|
21
|
|
|
try { |
|
22
|
|
|
$value = json_decode($value, true); |
|
23
|
|
|
} catch(\Exception $e) { |
|
24
|
|
|
$fail('Unable to determine the value type'); |
|
25
|
|
|
|
|
26
|
|
|
return; |
|
27
|
|
|
} |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
|
|
// `upload_multiple` sends [[0 => null]] when user doesn't upload anything |
|
31
|
|
|
// assume that nothing changed on field so nothing is sent on the request. |
|
32
|
|
|
if (count($value) === 1 && empty($value[0])) { |
|
33
|
|
|
unset($this->data[$attribute]); |
|
34
|
|
|
$value = []; |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
$previousValues = $this->entry?->{$attribute} ?? []; |
|
38
|
|
|
if (is_string($previousValues)) { |
|
39
|
|
|
$previousValues = json_decode($previousValues, true); |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
$value = array_merge($previousValues, $value); |
|
43
|
|
|
|
|
44
|
|
|
// if user uploaded something add it to the data beeing validated. |
|
45
|
|
|
if (! empty($value)) { |
|
46
|
|
|
$this->data[$attribute] = $value; |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
if ($this->entry) { |
|
50
|
|
|
$filesDeleted = CrudPanelFacade::getRequest()->input('clear_'.$attribute) ?? []; |
|
|
|
|
|
|
51
|
|
|
|
|
52
|
|
|
$data = $this->data; |
|
53
|
|
|
$data[$attribute] = array_diff($value, $filesDeleted); |
|
54
|
|
|
|
|
55
|
|
|
$this->validateArrayData($attribute, $fail, $data); |
|
56
|
|
|
|
|
57
|
|
|
$this->validateItems($attribute, $value, $fail); |
|
58
|
|
|
|
|
59
|
|
|
return; |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
$this->validateArrayData($attribute, $fail); |
|
63
|
|
|
|
|
64
|
|
|
$this->validateItems($attribute, $value, $fail); |
|
65
|
|
|
} |
|
66
|
|
|
} |
|
67
|
|
|
|