<?php

namespace app\Http\Requests;

use Illuminate\Contracts\Validation\Validator;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Http\Exceptions\HttpResponseException;
use Illuminate\Validation\Rule;

class RegistEPaperRequest extends FormRequest
{
    public function authorize()
    {
        return true; // 確保這裡返回 true
    }

    /**
     * Get the validation rules that apply to the request.
     */
    public function rules(): array
    {
        return [
            'email' => 'required|email'
        ];
    }

    /**
     * Get custom messages for validator errors.
     */
    public function messages(): array
    {
        return [
            'email.required' => 'E-Mail必填',
            'email.email' => 'E-Mail格式錯誤',
        ];
    }

    /**
     * 處理驗證失敗的情況
     */
    protected function failedValidation(Validator $validator)
    {
        throw new HttpResponseException(response()->json([
            'result' => "failed",
            'message' => $validator->errors(),
        ], 422));
    }
}
