api.so-manager-dev.com/app/Http/Requests/Api/UserInformationHistoryIndexRequest.php
Your Name 0b4acd7475
All checks were successful
Deploy api / deploy (push) Successful in 22s
Batch & API
2026-01-16 19:28:13 +09:00

74 lines
2.7 KiB
PHP

<?php
namespace App\Http\Requests\Api;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Contracts\Validation\Validator;
use Illuminate\Http\Exceptions\HttpResponseException;
class UserInformationHistoryIndexRequest extends FormRequest
{
/**
* リクエスト認可
*/
public function authorize(): bool
{
return true;
}
/**
* バリデーションルール
*/
public function rules(): array
{
return [
'user_id' => 'nullable|integer|min:1',
'entry_date_from' => 'nullable|date_format:Y-m-d',
'entry_date_to' => 'nullable|date_format:Y-m-d|after_or_equal:entry_date_from',
'page' => 'nullable|integer|min:1',
'per_page' => 'nullable|integer|min:1|max:100',
];
}
/**
* バリデーションエラーメッセージ
*/
public function messages(): array
{
return [
'user_id.integer' => 'E03: user_id の値が不正です。整数で指定してください。',
'user_id.min' => 'E03: user_id の値が不正です。1以上の整数で指定してください。',
'entry_date_from.date_format' => 'E04: entry_date_from の日付形式が不正です。YYYY-MM-DD形式で指定してください。',
'entry_date_to.date_format' => 'E04: entry_date_to の日付形式が不正です。YYYY-MM-DD形式で指定してください。',
'entry_date_to.after_or_equal' => 'E04: entry_date_to は entry_date_from 以降の日付を指定してください。',
'page.integer' => 'E03: page の値が不正です。整数で指定してください。',
'page.min' => 'E03: page の値が不正です。1以上の整数で指定してください。',
'per_page.integer' => 'E03: per_page の値が不正です。整数で指定してください。',
'per_page.min' => 'E03: per_page の値が不正です。1以上の整数で指定してください。',
'per_page.max' => 'E03: per_page の値が不正です。最大値は100です。',
];
}
/**
* バリデーション失敗時の処理
*/
protected function failedValidation(Validator $validator)
{
$errors = $validator->errors()->first();
// エラーコード抽出
preg_match('/^(E\d{2}):/', $errors, $matches);
$errorCode = $matches[1] ?? 'E99';
$message = preg_replace('/^E\d{2}: /', '', $errors);
throw new HttpResponseException(
response()->json([
'error' => [
'code' => $errorCode,
'message' => $message
]
], 400)
);
}
}