Skip to content

Commit 34761d8

Browse files
committed
added regex to user requests
1 parent 61ecdd9 commit 34761d8

File tree

2 files changed

+34
-2
lines changed

2 files changed

+34
-2
lines changed

app/Http/Requests/StoreUserRequest.php

+13-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,16 @@
66

77
class StoreUserRequest extends FormRequest
88
{
9+
/**
10+
* Sanitize before rules()
11+
*/
12+
protected function sanitizeInput()
13+
{
14+
$input = $this->all();
15+
$input['name'] = preg_replace("~[\p{M}]~uis", "", $this->input('name'));
16+
$this->replace($input);
17+
}
18+
919
/**
1020
* Get the validation rules that apply to the request.
1121
*
@@ -17,7 +27,7 @@ public function rules()
1727
'name' => 'required|string|min:1|max:150',
1828
'email' => 'required|email:rfc,dns',
1929
'password' => 'required|string|min:8|max:128',
20-
'role' => 'required|string|min:1|max:150',
30+
'role' => 'required|string|min:1|max:150|regex:/^[a-zA-Z]+$/', // Regex for ASCII letters
2131
];
2232
}
2333

@@ -28,6 +38,8 @@ public function rules()
2838
*/
2939
protected function getValidatorInstance()
3040
{
41+
$this->sanitizeInput();
42+
3143
return parent::getValidatorInstance()->after(function () {
3244
// Check if password and confirm password match
3345
if ($this->input('password') != $this->input('confirm_password')) {

app/Http/Requests/UpdateUserRequest.php

+21-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,16 @@
66

77
class UpdateUserRequest extends FormRequest
88
{
9+
/**
10+
* Sanitize before rules()
11+
*/
12+
protected function sanitizeInput()
13+
{
14+
$input = $this->all();
15+
$input['name'] = preg_replace("~[\p{M}]~uis", "", $this->input('name'));
16+
$this->replace($input);
17+
}
18+
919
/**
1020
* Get the validation rules that apply to the request.
1121
*
@@ -17,7 +27,17 @@ public function rules()
1727
'name' => 'required|string|min:1|max:150',
1828
'email' => 'email:rfc,dns',
1929
'password' => 'string|min:8|max:128',
20-
'role' => 'string|min:1|max:150',
30+
'role' => 'string|min:1|max:150|regex:/^[a-zA-Z]+$/', // Regex for ASCII letters
2131
];
2232
}
33+
34+
/**
35+
* Validate request
36+
*
37+
* @return Illuminate\Foundation\Http\FormRequest::getValidatorInstance
38+
*/
39+
protected function getValidatorInstance()
40+
{
41+
$this->sanitizeInput();
42+
}
2343
}

0 commit comments

Comments
 (0)