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
|
|
|
return; |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
if (! array_key_exists($attribute, $this->data) && $entry) { |
36
|
|
|
return; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
$this->createValidator($attribute, $this->getFieldRules(), $value, $fail); |
40
|
|
|
|
41
|
|
|
if (! empty($value) && ! empty($this->getFileRules())) { |
42
|
|
|
$this->createValidator($attribute, $this->getFileRules(), $value, $fail); |
43
|
|
|
} |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
public static function field(string|array|ValidationRule|Rule $rules = []): self |
47
|
|
|
{ |
48
|
|
|
return parent::field($rules); |
|
|
|
|
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
protected function validateUploadInSubfield($attribute, $value, Closure $fail, $entry = null) |
52
|
|
|
{ |
53
|
|
|
$mainField = Str::before($attribute, '.'); |
54
|
|
|
$subfield = Str::afterLast($attribute, '.'); |
55
|
|
|
$row = (int)Str::before(Str::after($attribute, '.'), '.'); |
56
|
|
|
|
57
|
|
|
$values[$mainField] = Uploader::mergeFilesAndValuesRecursive($this->data[$mainField], $this->data['_order_'.$mainField] ?? []); |
|
|
|
|
58
|
|
|
|
59
|
|
|
if (! array_key_exists($subfield, $values[$mainField][$row]) && $entry) { |
60
|
|
|
return; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
$this->createValidator($subfield, $this->getFieldRules(), $values[$mainField][$row][$subfield] ?? null, $fail); |
64
|
|
|
|
65
|
|
|
if(!empty($value) && !empty($this->getFileRules())) { |
66
|
|
|
$this->createValidator($subfield, $this->getFileRules(), $values[$mainField][$row][$subfield] ?? null, $fail); |
67
|
|
|
} |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
protected function createValidator(string $attribute, array $rules, mixed $value, Closure $fail): void |
71
|
|
|
{ |
72
|
|
|
$validator = Validator::make([$attribute => $value], [ |
73
|
|
|
$attribute => $rules, |
74
|
|
|
], $this->validator->customMessages, $this->validator->customAttributes); |
|
|
|
|
75
|
|
|
|
76
|
|
|
if ($validator->fails()) { |
77
|
|
|
foreach ($validator->errors()->messages()[$attribute] as $message) { |
78
|
|
|
$fail($message)->translate(); |
79
|
|
|
} |
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
} |
83
|
|
|
|