refactor:拆分 API 路由与请求校验,统一 final 类和代码风格
This commit is contained in:
59
app/Http/Requests/Admin/AdminUserStoreRequest.php
Normal file
59
app/Http/Requests/Admin/AdminUserStoreRequest.php
Normal file
@@ -0,0 +1,59 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Requests\Admin;
|
||||
|
||||
use Illuminate\Support\Str;
|
||||
use Illuminate\Foundation\Http\FormRequest;
|
||||
|
||||
/**
|
||||
* 管理员用户创建请求。
|
||||
*
|
||||
* @see AdminUserStoreController
|
||||
*/
|
||||
final class AdminUserStoreRequest extends FormRequest
|
||||
{
|
||||
/**
|
||||
* Determine if the user is authorized to make this request.
|
||||
*/
|
||||
public function authorize(): bool
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Prepare the data for validation.
|
||||
*/
|
||||
protected function prepareForValidation(): void
|
||||
{
|
||||
$username = $this->input('username');
|
||||
if (is_string($username)) {
|
||||
$this->merge([
|
||||
'username' => Str::lower(trim($username)),
|
||||
]);
|
||||
}
|
||||
|
||||
if ($this->input('email') === '') {
|
||||
$this->merge([
|
||||
'email' => null,
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the validation rules that apply to the request.
|
||||
*
|
||||
* @return array<string, array<int, mixed>>
|
||||
*/
|
||||
public function rules(): array
|
||||
{
|
||||
return [
|
||||
'username' => ['required', 'string', 'min:2', 'max:64', 'regex:/^[a-zA-Z0-9._-]+$/u', 'unique:admin_users,username'],
|
||||
'nickname' => ['required', 'string', 'max:128'],
|
||||
'email' => ['nullable', 'string', 'email', 'max:255'],
|
||||
'password' => ['required', 'string', 'min:8', 'max:256'],
|
||||
'status' => ['sometimes', 'integer', 'in:0,1'],
|
||||
'role_slugs' => ['required', 'array', 'min:1'],
|
||||
'role_slugs.*' => ['string', 'max:64', 'distinct', 'exists:admin_roles,slug'],
|
||||
];
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user