Auth: convert auth views and verification email to Nova layout
This commit is contained in:
@@ -3,6 +3,7 @@
|
||||
namespace App\Http\Requests;
|
||||
|
||||
use App\Models\User;
|
||||
use App\Support\UsernamePolicy;
|
||||
use Illuminate\Foundation\Http\FormRequest;
|
||||
use Illuminate\Validation\Rule;
|
||||
|
||||
@@ -16,7 +17,7 @@ class ProfileUpdateRequest extends FormRequest
|
||||
public function rules(): array
|
||||
{
|
||||
return [
|
||||
'username' => ['sometimes', 'string', 'max:255'],
|
||||
'username' => ['sometimes', ...UsernameRequest::rulesFor((int) $this->user()->id)],
|
||||
'email' => [
|
||||
'required',
|
||||
'string',
|
||||
@@ -42,4 +43,13 @@ class ProfileUpdateRequest extends FormRequest
|
||||
'photo' => ['nullable', 'image', 'max:2048', 'mimes:jpg,jpeg,png,webp'],
|
||||
];
|
||||
}
|
||||
|
||||
protected function prepareForValidation(): void
|
||||
{
|
||||
if ($this->has('username')) {
|
||||
$this->merge([
|
||||
'username' => UsernamePolicy::normalize((string) $this->input('username')),
|
||||
]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
73
app/Http/Requests/UsernameRequest.php
Normal file
73
app/Http/Requests/UsernameRequest.php
Normal file
@@ -0,0 +1,73 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Requests;
|
||||
|
||||
use App\Models\User;
|
||||
use App\Support\UsernamePolicy;
|
||||
use Illuminate\Foundation\Http\FormRequest;
|
||||
use Illuminate\Validation\Rule;
|
||||
|
||||
class UsernameRequest extends FormRequest
|
||||
{
|
||||
public function authorize(): bool
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
protected function prepareForValidation(): void
|
||||
{
|
||||
if ($this->has('username')) {
|
||||
$this->merge([
|
||||
'username' => UsernamePolicy::normalize((string) $this->input('username')),
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
public function rules(): array
|
||||
{
|
||||
return [
|
||||
'username' => self::rulesFor($this->resolveIgnoreUserId()),
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<int, mixed>
|
||||
*/
|
||||
public static function rulesFor(?int $ignoreUserId = null): array
|
||||
{
|
||||
return [
|
||||
...self::formatRules(),
|
||||
Rule::unique(User::class, 'username')->ignore($ignoreUserId),
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<int, mixed>
|
||||
*/
|
||||
public static function formatRules(): array
|
||||
{
|
||||
return [
|
||||
'required',
|
||||
'string',
|
||||
'min:' . UsernamePolicy::min(),
|
||||
'max:' . UsernamePolicy::max(),
|
||||
'regex:' . UsernamePolicy::regex(),
|
||||
Rule::notIn(UsernamePolicy::reserved()),
|
||||
];
|
||||
}
|
||||
|
||||
private function resolveIgnoreUserId(): ?int
|
||||
{
|
||||
$user = $this->user();
|
||||
if ($user) {
|
||||
return (int) $user->id;
|
||||
}
|
||||
|
||||
$routeUserId = $this->route('id') ?? $this->route('user');
|
||||
if (is_numeric($routeUserId)) {
|
||||
return (int) $routeUserId;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user