ValidatesRequests.php
2.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
<?php
namespace Illuminate\Foundation\Validation;
use Illuminate\Contracts\Validation\Factory;
use Illuminate\Http\Request;
use Illuminate\Validation\ValidationException;
trait ValidatesRequests
{
/**
* Run the validation routine against the given validator.
*
* @param \Illuminate\Contracts\Validation\Validator|array $validator
* @param \Illuminate\Http\Request|null $request
* @return array
*
* @throws \Illuminate\Validation\ValidationException
*/
public function validateWith($validator, Request $request = null)
{
$request = $request ?: request();
if (is_array($validator)) {
$validator = $this->getValidationFactory()->make($request->all(), $validator);
}
return $validator->validate();
}
/**
* Validate the given request with the given rules.
*
* @param \Illuminate\Http\Request $request
* @param array $rules
* @param array $messages
* @param array $customAttributes
* @return array
*
* @throws \Illuminate\Validation\ValidationException
*/
public function validate(Request $request, array $rules,
array $messages = [], array $customAttributes = [])
{
return $this->getValidationFactory()->make(
$request->all(), $rules, $messages, $customAttributes
)->validate();
}
/**
* Validate the given request with the given rules.
*
* @param string $errorBag
* @param \Illuminate\Http\Request $request
* @param array $rules
* @param array $messages
* @param array $customAttributes
* @return array
*
* @throws \Illuminate\Validation\ValidationException
*/
public function validateWithBag($errorBag, Request $request, array $rules,
array $messages = [], array $customAttributes = [])
{
try {
return $this->validate($request, $rules, $messages, $customAttributes);
} catch (ValidationException $e) {
$e->errorBag = $errorBag;
throw $e;
}
}
/**
* Get a validation factory instance.
*
* @return \Illuminate\Contracts\Validation\Factory
*/
protected function getValidationFactory()
{
return app(Factory::class);
}
}