|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Larapie\Actions\Concerns; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Contracts\Validation\Factory as ValidationFactory; |
|
6
|
|
|
use Illuminate\Validation\ValidationException; |
|
7
|
|
|
use Illuminate\Validation\Validator; |
|
8
|
|
|
|
|
9
|
|
|
trait ResolvesValidation |
|
10
|
|
|
{ |
|
11
|
|
|
protected $errorBag = 'default'; |
|
12
|
|
|
|
|
13
|
|
|
protected $validator; |
|
14
|
|
|
|
|
15
|
1 |
|
public function validate($rules = [], $messages = [], $customAttributes = []) |
|
16
|
|
|
{ |
|
17
|
1 |
|
return app(ValidationFactory::class) |
|
18
|
1 |
|
->make($this->validationData(), $rules, $messages, $customAttributes) |
|
19
|
1 |
|
->validate(); |
|
20
|
|
|
} |
|
21
|
|
|
|
|
22
|
41 |
|
public function passesValidation() |
|
23
|
|
|
{ |
|
24
|
41 |
|
return $this->getValidatorInstance()->passes(); |
|
25
|
|
|
} |
|
26
|
|
|
|
|
27
|
41 |
|
public function setValidator(Validator $validator) |
|
28
|
|
|
{ |
|
29
|
41 |
|
$this->validator = $validator; |
|
30
|
|
|
|
|
31
|
41 |
|
return $this; |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
1 |
|
public function validated() |
|
35
|
|
|
{ |
|
36
|
1 |
|
return $this->validator->validated(); |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
35 |
|
public function rules() |
|
40
|
|
|
{ |
|
41
|
35 |
|
return []; |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
40 |
|
public function messages() |
|
45
|
|
|
{ |
|
46
|
40 |
|
return []; |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
40 |
|
public function attributes() |
|
50
|
|
|
{ |
|
51
|
40 |
|
return []; |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
39 |
|
protected function resolveValidation() |
|
55
|
|
|
{ |
|
56
|
39 |
|
if (! $this->passesValidation()) { |
|
57
|
4 |
|
$this->failedValidation(); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
35 |
|
return $this; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
41 |
|
protected function getValidatorInstance() |
|
64
|
|
|
{ |
|
65
|
41 |
|
if ($this->validator) { |
|
66
|
3 |
|
return $this->validator; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
41 |
|
$factory = app(ValidationFactory::class); |
|
70
|
|
|
|
|
71
|
41 |
|
$validator = method_exists($this, 'validator') |
|
72
|
1 |
|
? $this->validator($factory) |
|
73
|
41 |
|
: $this->createDefaultValidator($factory); |
|
74
|
|
|
|
|
75
|
41 |
|
if (method_exists($this, 'withValidator')) { |
|
76
|
1 |
|
$this->resolveAndCall($this, 'withValidator', compact('validator')); |
|
|
|
|
|
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
41 |
|
if (method_exists($this, 'afterValidator')) { |
|
80
|
|
|
$validator->after(function ($validator) { |
|
81
|
1 |
|
$this->resolveAndCall($this, 'afterValidator', compact('validator')); |
|
82
|
1 |
|
}); |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
41 |
|
$this->setValidator($validator); |
|
86
|
|
|
|
|
87
|
41 |
|
return $this->validator; |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
40 |
|
protected function createDefaultValidator(ValidationFactory $factory) |
|
91
|
|
|
{ |
|
92
|
40 |
|
return $factory->make( |
|
93
|
40 |
|
$this->validationData(), $this->rules(), |
|
94
|
40 |
|
$this->messages(), $this->attributes() |
|
95
|
|
|
); |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
4 |
|
protected function failedValidation() |
|
99
|
|
|
{ |
|
100
|
4 |
|
throw new \Larapie\Actions\Exception\ValidationException($this->validator); |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
protected function getRedirectUrl() |
|
104
|
|
|
{ |
|
105
|
|
|
return redirect()->getUrlGenerator()->previous(); |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
40 |
|
protected function validationData() |
|
109
|
|
|
{ |
|
110
|
40 |
|
return $this->all(); |
|
|
|
|
|
|
111
|
|
|
} |
|
112
|
|
|
} |
|
113
|
|
|
|