59 lines
1.9 KiB
PHP
59 lines
1.9 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Requests;
|
|
|
|
use Illuminate\Foundation\Http\FormRequest;
|
|
use Illuminate\Support\Facades\Auth;
|
|
|
|
class AnnouncementRequest extends FormRequest
|
|
{
|
|
/**
|
|
* Determine if the user is authorized to make this request.
|
|
*/
|
|
public function authorize(): bool
|
|
{
|
|
return Auth::check() && is_role(['1']);
|
|
}
|
|
|
|
/**
|
|
* Get the validation rules that apply to the request.
|
|
*
|
|
* @return array<string, \Illuminate\Contracts\Validation\ValidationRule|array<mixed>|string>
|
|
*/
|
|
public function rules(): array
|
|
{
|
|
return [
|
|
'headline' => ['required'],
|
|
'user_ids' => ['required', 'array'],
|
|
'user_ids.*' => ['required', 'exists:users,id'],
|
|
'content' => ['required'],
|
|
'link' => ['nullable', 'url'],
|
|
'attachment' => ['nullable', 'file', 'mimes:pdf', 'max:2048'],
|
|
'enhancer_id' => ['required'],
|
|
];
|
|
}
|
|
|
|
public function messages(): array
|
|
{
|
|
return [
|
|
'headline.required' => 'Judul pengumuman harus diisi.',
|
|
'user_ids.required' => 'Pilih setidaknya satu media untuk pengumuman.',
|
|
'user_ids.array' => 'Media harus dalam format array.',
|
|
'user_ids.*.required' => 'Media yang dipilih tidak boleh kosong.',
|
|
'user_ids.*.exists' => 'Salah satu media yang dipilih tidak ditemukan.',
|
|
'content.required' => 'Konten pengumuman harus diisi.',
|
|
'link.url' => 'Format tautan/link tidak valid. Pastikan menggunakan format lengkap, misal: https://example.com.',
|
|
'attachment.file' => 'Lampiran harus berupa file.',
|
|
'attachment.mimes' => 'Lampiran hanya boleh berupa file PDF.',
|
|
'attachment.max' => 'Ukuran lampiran maksimal 2MB.',
|
|
];
|
|
}
|
|
|
|
protected function prepareForValidation()
|
|
{
|
|
$this->merge([
|
|
'enhancer_id' => Auth::check() ? Auth::id() : null,
|
|
]);
|
|
}
|
|
}
|