73 lines
2.3 KiB
PHP
73 lines
2.3 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Requests\Admin;
|
|
|
|
use Illuminate\Foundation\Http\FormRequest;
|
|
use Illuminate\Contracts\Validation\Validator;
|
|
use Illuminate\Http\Exceptions\HttpResponseException;
|
|
|
|
class FotoRequest extends FormRequest
|
|
{
|
|
public function authorize(): bool
|
|
{
|
|
return true; // Izinkan request
|
|
}
|
|
|
|
public function rules(): array
|
|
{
|
|
return [
|
|
'nama' => 'required|string|min:3|max:100',
|
|
'harga' => 'required|numeric|min:0',
|
|
'durasi' => 'required|integer|min:0',
|
|
'deskripsi' => 'required|string|min:10',
|
|
// Foto wajib saat Tambah (POST), opsional saat Edit (PUT)
|
|
'foto' => $this->isMethod('post')
|
|
? 'required|image|mimes:jpeg,png,jpg|max:2048'
|
|
: 'nullable|image|mimes:jpeg,png,jpg|max:2048',
|
|
];
|
|
}
|
|
|
|
public function messages(): array
|
|
{
|
|
return [
|
|
'required' => 'Kolom :attribute wajib diisi.',
|
|
'string' => 'Input :attribute harus berupa teks valid.',
|
|
'integer' => 'Input :attribute harus berupa angka valid.',
|
|
'numeric' => ':attribute harus berupa angka.',
|
|
'min' => ':attribute minimal harus :min karakter/nilai.',
|
|
'max' => ':attribute melebihi batas, maksimal :max karakter/KB.',
|
|
'image' => ':attribute harus berupa file gambar (foto).',
|
|
'mimes' => 'Format :attribute tidak didukung (jpeg, png, jpg).',
|
|
'foto.max' => 'Ukuran :attribute maksimal 2MB.',
|
|
];
|
|
}
|
|
|
|
public function attributes(): array
|
|
{
|
|
return [
|
|
'nama' => 'nama paket',
|
|
'harga' => 'harga paket',
|
|
'durasi' => 'durasi paket',
|
|
'foto' => 'foto paket',
|
|
];
|
|
}
|
|
|
|
/**
|
|
* Penanganan khusus untuk Modal di Admin
|
|
*/
|
|
protected function failedValidation(Validator $validator)
|
|
{
|
|
$redirect = redirect()->back()->withErrors($validator)->withInput();
|
|
|
|
if ($this->isMethod('post')) {
|
|
// Trigger modal tambah foto
|
|
$redirect->with('error_modal', 'createFoto');
|
|
} else {
|
|
// Trigger modal edit berdasarkan ID paket
|
|
$redirect->with('error_id_foto', $this->route('foto'));
|
|
}
|
|
|
|
throw new HttpResponseException($redirect);
|
|
}
|
|
}
|