77 lines
3.0 KiB
PHP
77 lines
3.0 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Requests;
|
|
|
|
use Illuminate\Foundation\Http\FormRequest;
|
|
use Illuminate\Support\Facades\Auth;
|
|
|
|
class ContentRecapRequest extends FormRequest
|
|
{
|
|
/**
|
|
* Determine if the user is authorized to make this request.
|
|
*/
|
|
public function authorize(): bool
|
|
{
|
|
return Auth::check() && is_role(['1', '2']);
|
|
}
|
|
|
|
/**
|
|
* Get the validation rules that apply to the request.
|
|
*
|
|
* @return array<string, \Illuminate\Contracts\Validation\ValidationRule|array<mixed>|string>
|
|
*/
|
|
public function rules(): array
|
|
{
|
|
$rules = [
|
|
'content_theme' => ['required'],
|
|
'posted_date' => ['required', 'date'],
|
|
'channel' => ['required', 'in:website,tiktok,youtube,instagram,facebook,twitter,cetak', 'not_in:placeholder'],
|
|
'link' => ['required', 'url'],
|
|
'classification_id' => ['required', 'exists:content_recap_classifications,id', 'not_in:placeholder'],
|
|
'social_media' => ['required', 'not_in:placeholder'],
|
|
'content_type' => ['required'],
|
|
'description' => ['nullable'],
|
|
'enhancer_id' => ['required'],
|
|
];
|
|
|
|
if ($this->isMethod('post')) {
|
|
$rules['image'] = ['required', 'image', 'mimes:png,jpg,jpeg', 'max:2048'];
|
|
} elseif ($this->isMethod('put')) {
|
|
$rules['image'] = ['nullable', 'image', 'mimes:png,jpg,jpeg', 'max:2048'];
|
|
}
|
|
|
|
return $rules;
|
|
}
|
|
|
|
protected function prepareForValidation()
|
|
{
|
|
$this->merge([
|
|
'image' => filled($this->image) ? $this->image : null,
|
|
'description' => filled($this->description) ? $this->description : null,
|
|
'enhancer_id' => Auth::check() ? Auth::id() : null,
|
|
]);
|
|
}
|
|
|
|
public function messages()
|
|
{
|
|
return [
|
|
'content_theme.required' => 'Tema konten wajib diisi.',
|
|
'content_theme.max' => 'Tema konten tidak boleh lebih dari 200 karakter.',
|
|
'posted_date.required' => 'Tanggal posting wajib diisi.',
|
|
'posted_date.date' => 'Tanggal posting harus berupa format tanggal yang valid.',
|
|
'channel.required' => 'Kanal wajib dipilih.',
|
|
'channel.in' => 'Kanal harus salah satu dari: website, tiktok, youtube, instagram, facebook, twitter, atau cetak.',
|
|
'link.required' => 'Link wajib diisi.',
|
|
'link.url' => 'Link harus berupa URL yang valid.',
|
|
'classification_id.required' => 'Klasifikasi wajib dipilih.',
|
|
'classification_id.exists' => 'Klasifikasi tidak ditemukan dalam data yang tersedia.',
|
|
'social_media.required' => 'Media sosial wajib diisi.',
|
|
'image.required' => 'Gambar wajib diunggah.',
|
|
'image.image' => 'File yang diunggah harus berupa gambar.',
|
|
'image.mimes' => 'Format gambar harus PNG, JPG, atau JPEG.',
|
|
'image.max' => 'Ukuran gambar maksimal 2MB.',
|
|
'description.nullable' => 'Deskripsi bersifat opsional.',
|
|
];
|
|
}
|
|
}
|