simedkom/app/Http/Requests/NewsRequest.php
2025-10-13 08:35:36 +07:00

65 lines
2.0 KiB
PHP

<?php
namespace App\Http\Requests;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Str;
class NewsRequest 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
{
$rules = [
'title' => ['required', 'max:200'],
'content' => ['required'],
'link' => ['nullable', 'url'],
'tag' => ['nullable'],
'category' => ['required', 'in:0,1'],
'image' => ['nullable', 'image', 'mimes:png,jpg,jpeg', 'max:2048'],
'slug' => ['required'],
'author_id' => ['required'],
];
return $rules;
}
public function messages(): array
{
return [
'title.required' => 'Judul harus diisi.',
'title.max' => 'Judul tidak boleh lebih dari 200 karakter.',
'content.required' => 'Konten harus diisi.',
'link.url' => 'Harus link valid.',
'category.required' => 'Kategori harus dipilih.',
'category.in' => 'Kategori harus bernilai berita atau pengumuman.',
'image.required' => 'Gambar wajib diunggah.',
'image.image' => 'File yang diunggah harus berupa gambar.',
'image.mimes' => 'Format gambar yang diperbolehkan adalah PNG, JPG, atau JPEG.',
'image.max' => 'Ukuran gambar tidak boleh lebih dari 4MB.',
];
}
protected function prepareForValidation()
{
$this->merge([
'slug' => filled($this->title) ? Str::slug($this->title) : null,
'author_id' => Auth::check() ? Auth::id() : null,
]);
}
}