|
1
|
|
|
<?php namespace Arcanedev\Support\Bases; |
|
2
|
|
|
|
|
3
|
|
|
use Arcanedev\Support\Http\FormRequest as BaseFormRequest; |
|
4
|
|
|
use Illuminate\Contracts\Validation\Validator; |
|
5
|
|
|
use Illuminate\Http\JsonResponse; |
|
6
|
|
|
|
|
7
|
|
|
/** |
|
8
|
|
|
* Class FormRequest |
|
9
|
|
|
* |
|
10
|
|
|
* @package Arcanedev\Support\Laravel |
|
11
|
|
|
* @author ARCANEDEV <[email protected]> |
|
12
|
|
|
* |
|
13
|
|
|
* @deprecated: Use `Arcanesoft\Support\Http\FormRequest` or `Arcanedev\LaravelApiHelper\Http\FormRequest` instead. |
|
14
|
|
|
*/ |
|
15
|
|
|
abstract class FormRequest extends BaseFormRequest |
|
16
|
|
|
{ |
|
17
|
|
|
/* ------------------------------------------------------------------------------------------------ |
|
18
|
|
|
| Properties |
|
19
|
|
|
| ------------------------------------------------------------------------------------------------ |
|
20
|
|
|
*/ |
|
21
|
|
|
/** |
|
22
|
|
|
* Specify if the form request is an ajax request. |
|
23
|
|
|
* |
|
24
|
|
|
* @var bool |
|
25
|
|
|
*/ |
|
26
|
|
|
protected $ajaxRequest = false; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* The errors format. |
|
30
|
|
|
* |
|
31
|
|
|
* @var string|null |
|
32
|
|
|
*/ |
|
33
|
|
|
protected $errorsFormat = null; |
|
34
|
|
|
|
|
35
|
|
|
/* ----------------------------------------------------------------- |
|
36
|
|
|
| Main Methods |
|
37
|
|
|
| ----------------------------------------------------------------- |
|
38
|
|
|
*/ |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* Get the proper failed validation response for the request. |
|
42
|
|
|
* |
|
43
|
|
|
* @param array $errors |
|
44
|
|
|
* |
|
45
|
|
|
* @return \Symfony\Component\HttpFoundation\Response |
|
46
|
|
|
*/ |
|
47
|
|
|
public function response(array $errors) |
|
48
|
|
|
{ |
|
49
|
|
|
return $this->ajaxRequest |
|
50
|
|
|
? $this->formatJsonErrorsResponse($errors) |
|
51
|
|
|
: parent::response($errors); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/* ------------------------------------------------------------------------------------------------ |
|
55
|
|
|
| Other Functions |
|
56
|
|
|
| ------------------------------------------------------------------------------------------------ |
|
57
|
|
|
*/ |
|
58
|
|
|
/** |
|
59
|
|
|
* Format the json response. |
|
60
|
|
|
* |
|
61
|
|
|
* @param array $errors |
|
62
|
|
|
* |
|
63
|
|
|
* @return \Symfony\Component\HttpFoundation\Response |
|
64
|
|
|
*/ |
|
65
|
|
|
protected function formatJsonErrorsResponse(array $errors) |
|
66
|
|
|
{ |
|
67
|
|
|
return new JsonResponse([ |
|
68
|
|
|
'status' => 'error', |
|
69
|
|
|
'code' => 422, |
|
70
|
|
|
'errors' => array_map('reset', $errors) |
|
71
|
|
|
], 422); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* {@inheritdoc} |
|
76
|
|
|
*/ |
|
77
|
|
|
protected function formatErrors(Validator $validator) |
|
78
|
|
|
{ |
|
79
|
|
|
if (is_null($this->errorsFormat)) { |
|
80
|
|
|
return parent::formatErrors($validator); |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
$errors = []; |
|
84
|
|
|
$messages = $validator->getMessageBag(); |
|
85
|
|
|
|
|
86
|
|
|
foreach ($messages->keys() as $key) { |
|
87
|
|
|
$errors[$key] = $messages->get($key, $this->errorsFormat); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
return $errors; |
|
91
|
|
|
} |
|
92
|
|
|
} |
|
93
|
|
|
|