Skip to content

Commit 6db95bb

Browse files
committed
Move validation to form requests
1 parent f1c4c46 commit 6db95bb

File tree

7 files changed

+125
-18
lines changed

7 files changed

+125
-18
lines changed

app/Http/Controllers/Auth/ConfirmablePasswordController.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace App\Http\Controllers\Auth;
44

55
use App\Http\Controllers\Controller;
6+
use App\Http\Requests\Auth\ConfirmPasswordRequest;
67
use Illuminate\Http\RedirectResponse;
78
use Illuminate\Http\Request;
89
use Illuminate\Support\Facades\Auth;
@@ -23,7 +24,7 @@ public function show(): Response
2324
/**
2425
* Confirm the user's password.
2526
*/
26-
public function store(Request $request): RedirectResponse
27+
public function store(ConfirmPasswordRequest $request): RedirectResponse
2728
{
2829
if (! Auth::guard('web')->validate([
2930
'email' => $request->user()->email,

app/Http/Controllers/Auth/NewPasswordController.php

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,13 @@
33
namespace App\Http\Controllers\Auth;
44

55
use App\Http\Controllers\Controller;
6+
use App\Http\Requests\Auth\ResetPasswordRequest;
67
use Illuminate\Auth\Events\PasswordReset;
78
use Illuminate\Http\RedirectResponse;
89
use Illuminate\Http\Request;
910
use Illuminate\Support\Facades\Hash;
1011
use Illuminate\Support\Facades\Password;
1112
use Illuminate\Support\Str;
12-
use Illuminate\Validation\Rules;
1313
use Illuminate\Validation\ValidationException;
1414
use Inertia\Inertia;
1515
use Inertia\Response;
@@ -32,14 +32,8 @@ public function create(Request $request): Response
3232
*
3333
* @throws \Illuminate\Validation\ValidationException
3434
*/
35-
public function store(Request $request): RedirectResponse
35+
public function store(ResetPasswordRequest $request): RedirectResponse
3636
{
37-
$request->validate([
38-
'token' => 'required',
39-
'email' => 'required|email',
40-
'password' => ['required', 'confirmed', Rules\Password::defaults()],
41-
]);
42-
4337
// Here we will attempt to reset the user's password. If it is successful we
4438
// will update the password on an actual user model and persist it to the
4539
// database. Otherwise we will parse the error and return the response.

app/Http/Controllers/Auth/RegisteredUserController.php

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,12 @@
33
namespace App\Http\Controllers\Auth;
44

55
use App\Http\Controllers\Controller;
6+
use App\Http\Requests\Auth\RegisterRequest;
67
use App\Models\User;
78
use Illuminate\Auth\Events\Registered;
89
use Illuminate\Http\RedirectResponse;
9-
use Illuminate\Http\Request;
1010
use Illuminate\Support\Facades\Auth;
1111
use Illuminate\Support\Facades\Hash;
12-
use Illuminate\Validation\Rules;
1312
use Inertia\Inertia;
1413
use Inertia\Response;
1514

@@ -28,14 +27,8 @@ public function create(): Response
2827
*
2928
* @throws \Illuminate\Validation\ValidationException
3029
*/
31-
public function store(Request $request): RedirectResponse
30+
public function store(RegisterRequest $request): RedirectResponse
3231
{
33-
$request->validate([
34-
'name' => 'required|string|max:255',
35-
'email' => 'required|string|lowercase|email|max:255|unique:'.User::class,
36-
'password' => ['required', 'confirmed', Rules\Password::defaults()],
37-
]);
38-
3932
$user = User::create([
4033
'name' => $request->name,
4134
'email' => $request->email,
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
namespace App\Http\Requests\Auth;
4+
5+
use Illuminate\Foundation\Http\FormRequest;
6+
7+
class ConfirmPasswordRequest extends FormRequest
8+
{
9+
/**
10+
* Determine if the user is authorized to make this request.
11+
*/
12+
public function authorize(): bool
13+
{
14+
return true;
15+
}
16+
17+
/**
18+
* Get the validation rules that apply to the request.
19+
*
20+
* @return array<string, \Illuminate\Contracts\Validation\ValidationRule|array<mixed>|string>
21+
*/
22+
public function rules(): array
23+
{
24+
return [
25+
'password' => ['required', 'string'],
26+
];
27+
}
28+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
namespace App\Http\Requests\Auth;
4+
5+
use Illuminate\Foundation\Http\FormRequest;
6+
7+
class ForgotPasswordRequest extends FormRequest
8+
{
9+
/**
10+
* Determine if the user is authorized to make this request.
11+
*/
12+
public function authorize(): bool
13+
{
14+
return true;
15+
}
16+
17+
/**
18+
* Get the validation rules that apply to the request.
19+
*
20+
* @return array<string, \Illuminate\Contracts\Validation\ValidationRule|array<mixed>|string>
21+
*/
22+
public function rules(): array
23+
{
24+
return [
25+
'email' => ['required', 'email'],
26+
];
27+
}
28+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
3+
namespace App\Http\Requests\Auth;
4+
5+
use App\Models\User;
6+
use Illuminate\Foundation\Http\FormRequest;
7+
use Illuminate\Validation\Rules\Password;
8+
9+
class RegisterRequest extends FormRequest
10+
{
11+
/**
12+
* Determine if the user is authorized to make this request.
13+
*/
14+
public function authorize(): bool
15+
{
16+
return true;
17+
}
18+
19+
/**
20+
* Get the validation rules that apply to the request.
21+
*
22+
* @return array<string, \Illuminate\Contracts\Validation\ValidationRule|array<mixed>|string>
23+
*/
24+
public function rules(): array
25+
{
26+
return [
27+
'name' => 'required|string|max:255',
28+
'email' => 'required|string|lowercase|email|max:255|unique:'.User::class,
29+
'password' => ['required', 'confirmed', Password::defaults()],
30+
];
31+
}
32+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
namespace App\Http\Requests\Auth;
4+
5+
use Illuminate\Foundation\Http\FormRequest;
6+
use Illuminate\Validation\Rules\Password;
7+
8+
class ResetPasswordRequest extends FormRequest
9+
{
10+
/**
11+
* Determine if the user is authorized to make this request.
12+
*/
13+
public function authorize(): bool
14+
{
15+
return true;
16+
}
17+
18+
/**
19+
* Get the validation rules that apply to the request.
20+
*
21+
* @return array<string, \Illuminate\Contracts\Validation\ValidationRule|array<mixed>|string>
22+
*/
23+
public function rules(): array
24+
{
25+
return [
26+
'token' => 'required',
27+
'email' => 'required|email',
28+
'password' => ['required', 'confirmed', Password::defaults()],
29+
];
30+
}
31+
}

0 commit comments

Comments
 (0)