Skip to content

Commit b480078

Browse files
Add Concerns namespace and refactor code to reuse traits for rules (#204)
* Add Concerns namespace and refactor code to reuse traits for rules * Create profileRules method for trait
1 parent a0b712e commit b480078

File tree

7 files changed

+79
-28
lines changed

7 files changed

+79
-28
lines changed

app/Actions/Fortify/CreateNewUser.php

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,15 @@
22

33
namespace App\Actions\Fortify;
44

5+
use App\Concerns\PasswordValidationRules;
6+
use App\Concerns\ProfileValidationRules;
57
use App\Models\User;
68
use Illuminate\Support\Facades\Validator;
7-
use Illuminate\Validation\Rule;
89
use Laravel\Fortify\Contracts\CreatesNewUsers;
910

1011
class CreateNewUser implements CreatesNewUsers
1112
{
12-
use PasswordValidationRules;
13+
use PasswordValidationRules, ProfileValidationRules;
1314

1415
/**
1516
* Validate and create a newly registered user.
@@ -19,14 +20,7 @@ class CreateNewUser implements CreatesNewUsers
1920
public function create(array $input): User
2021
{
2122
Validator::make($input, [
22-
'name' => ['required', 'string', 'max:255'],
23-
'email' => [
24-
'required',
25-
'string',
26-
'email',
27-
'max:255',
28-
Rule::unique(User::class),
29-
],
23+
...$this->profileRules(),
3024
'password' => $this->passwordRules(),
3125
])->validate();
3226

app/Actions/Fortify/ResetUserPassword.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace App\Actions\Fortify;
44

5+
use App\Concerns\PasswordValidationRules;
56
use App\Models\User;
67
use Illuminate\Support\Facades\Validator;
78
use Laravel\Fortify\Contracts\ResetsUserPasswords;

app/Actions/Fortify/PasswordValidationRules.php renamed to app/Concerns/PasswordValidationRules.php

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22

3-
namespace App\Actions\Fortify;
3+
namespace App\Concerns;
44

55
use Illuminate\Validation\Rules\Password;
66

@@ -15,4 +15,14 @@ protected function passwordRules(): array
1515
{
1616
return ['required', 'string', Password::default(), 'confirmed'];
1717
}
18+
19+
/**
20+
* Get the validation rules used to validate the current password.
21+
*
22+
* @return array<int, \Illuminate\Contracts\Validation\Rule|array<mixed>|string>
23+
*/
24+
protected function currentPasswordRules(): array
25+
{
26+
return ['required', 'string', 'current_password'];
27+
}
1828
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
<?php
2+
3+
namespace App\Concerns;
4+
5+
use App\Models\User;
6+
use Illuminate\Validation\Rule;
7+
8+
trait ProfileValidationRules
9+
{
10+
/**
11+
* Get the validation rules used to validate user profiles.
12+
*
13+
* @return array<string, array<int, \Illuminate\Contracts\Validation\Rule|array<mixed>|string>>
14+
*/
15+
protected function profileRules(?int $userId = null): array
16+
{
17+
return [
18+
'name' => $this->nameRules(),
19+
'email' => $this->emailRules($userId),
20+
];
21+
}
22+
23+
/**
24+
* Get the validation rules used to validate user names.
25+
*
26+
* @return array<int, \Illuminate\Contracts\Validation\Rule|array<mixed>|string>
27+
*/
28+
protected function nameRules(): array
29+
{
30+
return ['required', 'string', 'max:255'];
31+
}
32+
33+
/**
34+
* Get the validation rules used to validate user emails.
35+
*
36+
* @return array<int, \Illuminate\Contracts\Validation\Rule|array<mixed>|string>
37+
*/
38+
protected function emailRules(?int $userId = null): array
39+
{
40+
return [
41+
'required',
42+
'string',
43+
'email',
44+
'max:255',
45+
$userId === null
46+
? Rule::unique(User::class)
47+
: Rule::unique(User::class)->ignore($userId),
48+
];
49+
}
50+
}

app/Livewire/Settings/DeleteUserForm.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,15 @@
22

33
namespace App\Livewire\Settings;
44

5+
use App\Concerns\PasswordValidationRules;
56
use App\Livewire\Actions\Logout;
67
use Illuminate\Support\Facades\Auth;
78
use Livewire\Component;
89

910
class DeleteUserForm extends Component
1011
{
12+
use PasswordValidationRules;
13+
1114
public string $password = '';
1215

1316
/**
@@ -16,7 +19,7 @@ class DeleteUserForm extends Component
1619
public function deleteUser(Logout $logout): void
1720
{
1821
$this->validate([
19-
'password' => ['required', 'string', 'current_password'],
22+
'password' => $this->currentPasswordRules(),
2023
]);
2124

2225
tap(Auth::user(), $logout(...))->delete();

app/Livewire/Settings/Password.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,15 @@
22

33
namespace App\Livewire\Settings;
44

5+
use App\Concerns\PasswordValidationRules;
56
use Illuminate\Support\Facades\Auth;
6-
use Illuminate\Validation\Rules\Password as PasswordRule;
77
use Illuminate\Validation\ValidationException;
88
use Livewire\Component;
99

1010
class Password extends Component
1111
{
12+
use PasswordValidationRules;
13+
1214
public string $current_password = '';
1315

1416
public string $password = '';
@@ -22,8 +24,8 @@ public function updatePassword(): void
2224
{
2325
try {
2426
$validated = $this->validate([
25-
'current_password' => ['required', 'string', 'current_password'],
26-
'password' => ['required', 'string', PasswordRule::defaults(), 'confirmed'],
27+
'current_password' => $this->currentPasswordRules(),
28+
'password' => $this->passwordRules(),
2729
]);
2830
} catch (ValidationException $e) {
2931
$this->reset('current_password', 'password', 'password_confirmation');

app/Livewire/Settings/Profile.php

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,16 @@
22

33
namespace App\Livewire\Settings;
44

5+
use App\Concerns\ProfileValidationRules;
56
use App\Models\User;
67
use Illuminate\Support\Facades\Auth;
78
use Illuminate\Support\Facades\Session;
8-
use Illuminate\Validation\Rule;
99
use Livewire\Component;
1010

1111
class Profile extends Component
1212
{
13+
use ProfileValidationRules;
14+
1315
public string $name = '';
1416

1517
public string $email = '';
@@ -30,18 +32,7 @@ public function updateProfileInformation(): void
3032
{
3133
$user = Auth::user();
3234

33-
$validated = $this->validate([
34-
'name' => ['required', 'string', 'max:255'],
35-
36-
'email' => [
37-
'required',
38-
'string',
39-
'lowercase',
40-
'email',
41-
'max:255',
42-
Rule::unique(User::class)->ignore($user->id),
43-
],
44-
]);
35+
$validated = $this->validate($this->profileRules($user->id));
4536

4637
$user->fill($validated);
4738

0 commit comments

Comments
 (0)