Skip to content

Commit 61ecdd9

Browse files
committed
added regexes for rentables
1 parent cec312a commit 61ecdd9

File tree

2 files changed

+28
-4
lines changed

2 files changed

+28
-4
lines changed

app/Http/Requests/StoreRentableRequest.php

+14-2
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,16 @@
66

77
class StoreRentableRequest extends FormRequest
88
{
9+
/**
10+
* Sanitize before rules()
11+
*/
12+
protected function sanitizeInput()
13+
{
14+
$input = $this->all();
15+
$input['description'] = preg_replace("~[\p{M}]~uis", "", $this->input('description'));
16+
$this->replace($input);
17+
}
18+
919
/**
1020
* Get the validation rules that apply to the request.
1121
*
@@ -17,12 +27,12 @@ public function rules()
1727

1828
return [
1929
'user_id' => 'required|integer|min:1|exists:App\Models\User,id',
20-
'adress' => 'required|string|min:3|max:150',
30+
'adress' => 'required|string|min:3|max:150|regex:/^[a-zA-Z0-9_ .-]*$/', // Regex for Adress
2131
'postal_code' => 'required|numeric|digits:4|min:1|max:9999',
2232
'date_of_hire' => 'required|date_format:Y-m-d|after_or_equal:' . $todayDate,
2333
'start_time' => 'required',
2434
'end_time' => 'required',
25-
'price' => 'required|numeric|min:0.01|max:1000',
35+
'price' => 'required|numeric|min:0.01|max:1000|regex:/^[0-9]+(\.[0-9]{1,2})?$/', //Regex for Decimal with 2 decimal places
2636
'bankaccount_nr' => 'required|string|regex:/^[A-Z]{2}(?:[ ]?[0-9]){14,20}$/', // Regex for IBAN numbers
2737
'description' => 'required|string|max:150',
2838
];
@@ -35,6 +45,8 @@ public function rules()
3545
*/
3646
protected function getValidatorInstance()
3747
{
48+
$this->sanitizeInput();
49+
3850
return parent::getValidatorInstance()->after(function () {
3951
// convert to unix timestamps
4052
$start_time = $this->input('start_time');

app/Http/Requests/UpdateRentableRequest.php

+14-2
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,16 @@ public function __construct(IRentableRepository $rentableRepo)
1414
$this->rentableRepo = $rentableRepo;
1515
}
1616

17+
/**
18+
* Sanitize before rules()
19+
*/
20+
protected function sanitizeInput()
21+
{
22+
$input = $this->all();
23+
$input['description'] = preg_replace("~[\p{M}]~uis", "", $this->input('description'));
24+
$this->replace($input);
25+
}
26+
1727
/**
1828
* Get the validation rules that apply to the request.
1929
*
@@ -25,12 +35,12 @@ public function rules()
2535

2636
return [
2737
'rentable_id' => 'required|integer|min:1|exists:App\Models\Rentable,id',
28-
'adress' => 'required|string|min:3|max:150',
38+
'adress' => 'required|string|min:3|max:150|regex:/^[a-zA-Z0-9_ .-]*$/', // Regex for Adress
2939
'postal_code' => 'required|numeric|digits:4|min:1|max:9999',
3040
'date_of_hire' => 'required|date_format:Y-m-d|after_or_equal:' . $todayDate,
3141
'start_time' => 'required',
3242
'end_time' => 'required',
33-
'price' => 'required|numeric|min:0.01|max:1000',
43+
'price' => 'required|numeric|min:0.01|max:1000|regex:/^[0-9]+(\.[0-9]{1,2})?$/', //Regex for Decimal with 2 decimal places
3444
'bankaccount_nr' => 'required|string|regex:/^[A-Z]{2}(?:[ ]?[0-9]){14,20}$/', // Regex for IBAN numbers
3545
'description' => 'required|string|max:150',
3646
];
@@ -43,6 +53,8 @@ public function rules()
4353
*/
4454
protected function getValidatorInstance()
4555
{
56+
$this->sanitizeInput();
57+
4658
return parent::getValidatorInstance()->after(function () {
4759
// Get the current rentable
4860
$rentable = $this->rentableRepo->getRentable($this->input('rentable_id'));

0 commit comments

Comments
 (0)