Newer
Older
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
<?php
namespace App\Http\Requests;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Validation\Rule;
class StatsByLocationRequest extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*/
public function authorize(): bool
{
return true;
}
/**
* Prepare the data for validation - sanitize inputs.
*/
protected function prepareForValidation(): void
{
$sanitized = [];
// Sanitize group_by: trim, lowercase, remove special characters
if ($this->has('group_by')) {
$sanitized['group_by'] = strtolower(
preg_replace('/[^a-z_]/', '', trim($this->group_by))
);
}
// Sanitize year: convert to integer, remove any non-numeric characters
if ($this->has('year')) {
$sanitized['year'] = (int) preg_replace('/[^0-9]/', '', $this->year);
}
$this->merge($sanitized);
}
/**
* Get the validation rules that apply to the request.
*/
public function rules(): array
{
return [
'group_by' => ['sometimes', 'string', 'alpha', Rule::in(['department', 'region'])],
'year' => 'sometimes|integer|min:2000|max:2100',
];
}
}