|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Backpack\CRUD\app\Library\Validation\Rules; |
|
4
|
|
|
|
|
5
|
|
|
use Backpack\CRUD\app\Library\CrudPanel\CrudPanelFacade; |
|
6
|
|
|
use Illuminate\Contracts\Validation\Rule; |
|
7
|
|
|
use Illuminate\Contracts\Validation\ValidationRule; |
|
8
|
|
|
use Illuminate\Support\Arr; |
|
9
|
|
|
use Illuminate\Support\Str; |
|
10
|
|
|
|
|
11
|
|
|
class ValidUpload extends BackpackCustomRule |
|
12
|
|
|
{ |
|
13
|
|
|
/** |
|
14
|
|
|
* Run the validation rule and return the array of errors. |
|
15
|
|
|
*/ |
|
16
|
|
|
public function validateRules(string $attribute, mixed $value): array |
|
17
|
|
|
{ |
|
18
|
|
|
$entry = CrudPanelFacade::getCurrentEntry(); |
|
|
|
|
|
|
19
|
|
|
|
|
20
|
|
|
if (! array_key_exists($attribute, $this->data) && $entry) { |
|
21
|
|
|
if (str_contains($attribute, '.') && get_class($entry) === get_class(CrudPanelFacade::getModel())) { |
|
|
|
|
|
|
22
|
|
|
$previousValue = Arr::get($this->data, '_order_'.Str::before($attribute, '.')); |
|
23
|
|
|
$previousValue = Arr::get($previousValue, Str::after($attribute, '.')); |
|
24
|
|
|
} else { |
|
25
|
|
|
$previousValue = Arr::get($entry, $attribute); |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
|
|
if ($previousValue && empty($value)) { |
|
29
|
|
|
return []; |
|
30
|
|
|
} |
|
31
|
|
|
Arr::set($this->data, $attribute, $previousValue ?? $value); |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
$fieldErrors = $this->validateFieldRules($attribute, $value); |
|
35
|
|
|
|
|
36
|
|
|
if (! empty($value) && ! empty($this->getFileRules())) { |
|
37
|
|
|
$fileErrors = $this->validateFileRules($attribute, $value); |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
return array_merge($fieldErrors, $fileErrors ?? []); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
public static function field(string|array|ValidationRule|Rule $rules = []): self |
|
44
|
|
|
{ |
|
45
|
|
|
return parent::field($rules); |
|
|
|
|
|
|
46
|
|
|
} |
|
47
|
|
|
} |
|
48
|
|
|
|