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 Backpack\CRUD\app\Library\Validation\Rules\Support\HasFiles; |
8
|
|
|
use Closure; |
9
|
|
|
use Illuminate\Contracts\Validation\Rule; |
10
|
|
|
use Illuminate\Contracts\Validation\ValidationRule; |
11
|
|
|
use Illuminate\Support\Facades\Validator; |
12
|
|
|
use Illuminate\Support\Str; |
13
|
|
|
|
14
|
|
|
class ValidUpload extends BackpackCustomRule |
15
|
|
|
{ |
16
|
|
|
use HasFiles; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Run the validation rule. |
20
|
|
|
* |
21
|
|
|
* @param string $attribute |
22
|
|
|
* @param mixed $value |
23
|
|
|
* @param Closure(string): \Illuminate\Translation\PotentiallyTranslatedString $fail |
24
|
|
|
* @return void |
25
|
|
|
*/ |
26
|
|
|
public function validate(string $attribute, mixed $value, Closure $fail): void |
27
|
|
|
{ |
28
|
|
|
$entry = CrudPanelFacade::getCurrentEntry(); |
|
|
|
|
29
|
|
|
|
30
|
|
|
if (Str::contains($attribute, '.')) { |
31
|
|
|
$this->validateUploadInSubfield($attribute, $value, $fail, $entry); |
32
|
|
|
|
33
|
|
|
return; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
if (! array_key_exists($attribute, $this->data) && $entry) { |
37
|
|
|
return; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
$this->createValidator($attribute, $this->getFieldRules(), $value, $fail); |
41
|
|
|
|
42
|
|
|
if (! empty($value) && ! empty($this->getFileRules())) { |
43
|
|
|
$this->createValidator($attribute, $this->getFileRules(), $value, $fail); |
44
|
|
|
} |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
public static function field(string|array|ValidationRule|Rule $rules = []): self |
48
|
|
|
{ |
49
|
|
|
return parent::field($rules); |
|
|
|
|
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
protected function validateUploadInSubfield($attribute, $value, Closure $fail, $entry = null) |
53
|
|
|
{ |
54
|
|
|
$mainField = Str::before($attribute, '.'); |
55
|
|
|
$subfield = Str::afterLast($attribute, '.'); |
56
|
|
|
$row = (int) Str::before(Str::after($attribute, '.'), '.'); |
57
|
|
|
|
58
|
|
|
$values[$mainField] = Uploader::mergeFilesAndValuesRecursive($this->data[$mainField], $this->data['_order_'.$mainField] ?? []); |
|
|
|
|
59
|
|
|
|
60
|
|
|
if (! array_key_exists($subfield, $values[$mainField][$row]) && $entry) { |
61
|
|
|
return; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
$this->createValidator($subfield, $this->getFieldRules(), $values[$mainField][$row][$subfield] ?? null, $fail); |
65
|
|
|
|
66
|
|
|
if (! empty($value) && ! empty($this->getFileRules())) { |
67
|
|
|
$this->createValidator($subfield, $this->getFileRules(), $values[$mainField][$row][$subfield] ?? null, $fail); |
68
|
|
|
} |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
protected function createValidator(string $attribute, array $rules, mixed $value, Closure $fail): void |
72
|
|
|
{ |
73
|
|
|
$validator = Validator::make([$attribute => $value], [ |
74
|
|
|
$attribute => $rules, |
75
|
|
|
], $this->validator->customMessages, $this->validator->customAttributes); |
|
|
|
|
76
|
|
|
|
77
|
|
|
if ($validator->fails()) { |
78
|
|
|
foreach ($validator->errors()->messages()[$attribute] as $message) { |
79
|
|
|
$fail($message)->translate(); |
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
|