75 lines
2.6 KiB
PHP
75 lines
2.6 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:50|regex:/^[a-zA-Z0-9\s\.\:\(\)\"\-\,]+$/',
|
|
'harga' => 'required|numeric|min:0',
|
|
'durasi' => 'required|integer|min:0',
|
|
'deskripsi' => 'required|string|min:3|max:170|regex:/^[a-zA-Z0-9\s\.\:\(\)\"\-\,]+$/',
|
|
'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' => ':attribute wajib diisi.',
|
|
'numeric' => ':attribute harus berupa angka.',
|
|
'string' => ':attribute harus berupa teks valid.',
|
|
'integer' => 'Input :attribute harus berupa angka valid.',
|
|
'numeric' => ':attribute harus berupa angka.',
|
|
'regex' => ':attribute hanya boleh berisi huruf, angka, spasi, titik, koma, titik dua, tanda hubung, tanda kurung, garis miring, dan tanda petik dua.',
|
|
'min' => ':attribute terlalu pendek, minimal :min karakter.',
|
|
'max' => ':attribute terlalu panjang, maksimal :max karakter.',
|
|
'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',
|
|
'deskripsi' => 'Deskripsi 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);
|
|
}
|
|
}
|