79 lines
3.0 KiB
PHP
79 lines
3.0 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Requests\Admin;
|
|
|
|
use Illuminate\Foundation\Http\FormRequest;
|
|
use Illuminate\Contracts\Validation\Validator;
|
|
use Illuminate\Http\Exceptions\HttpResponseException;
|
|
|
|
class BuketRequest extends FormRequest
|
|
{
|
|
public function authorize(): bool
|
|
{
|
|
// Ubah jadi true supaya request diizinkan
|
|
return true;
|
|
}
|
|
|
|
public function rules(): array
|
|
{
|
|
return [
|
|
//tmbah requirement
|
|
'nama' => 'required|string|min:3|max:100|regex:/^[a-zA-Z0-9\s\.\:\(\)\"\-\,]+$/',
|
|
'ukuran' => 'required|in:S,M,L',
|
|
'kategori' => 'required|in:single,fresh,premium_fresh,artificial',
|
|
'harga' => 'required|numeric|min:0',
|
|
'request_khusus' => 'required|string|min:3|max:100|regex:/^[a-zA-Z0-9\s\.\:\(\)\"\-\,]+$/',
|
|
'deskripsi' => 'required|string|min:3|min: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.',
|
|
'min' => ':attribute terlalu pendek, minimal :min karakter.',
|
|
'max' => ':attribute terlalu panjang, maksimal :max karakter.',
|
|
'in' => 'Pilihan :attribute tidak sesuai dengan data yang tersedia.',
|
|
'regex' => ':attribute hanya boleh berisi huruf, angka, spasi, titik, koma, titik dua, tanda hubung, tanda kurung, garis miring, dan tanda petik dua.',
|
|
'image' => ':attribute harus berupa file gambar (foto).',
|
|
'mimes' => 'Format :attribute tidak didukung. Gunakan format: jpeg, png, atau jpg.',
|
|
'foto.max' => 'Ukuran :attribute terlalu besar, maksimal adalah 2MB.',
|
|
];
|
|
}
|
|
|
|
public function attributes(): array
|
|
{
|
|
return [
|
|
'nama' => 'Nama buket',
|
|
'ukuran' => 'Ukuran buket',
|
|
'kategori' => 'Kategori buket',
|
|
'harga' => 'Harga buket',
|
|
'request_khusus' => 'Request khusus',
|
|
'deskripsi' => 'Deskripsi buket',
|
|
'foto' => 'Foto buket',
|
|
];
|
|
}
|
|
|
|
/**
|
|
* Trik untuk Modal: Mengembalikan error ke modal yang tepat
|
|
*/
|
|
protected function failedValidation(Validator $validator)
|
|
{
|
|
$redirect = redirect()->back()->withErrors($validator)->withInput();
|
|
|
|
if ($this->isMethod('post')) {
|
|
$redirect->with('error_modal', 'create');
|
|
} else {
|
|
// Mengambil ID dari parameter route untuk modal edit
|
|
$redirect->with('error_id', $this->route('buket'));
|
|
}
|
|
|
|
throw new HttpResponseException($redirect);
|
|
}
|
|
}
|