sementara
This commit is contained in:
114
app/Http/Requests/ReferensiLinkRequest.php
Normal file
114
app/Http/Requests/ReferensiLinkRequest.php
Normal file
@@ -0,0 +1,114 @@
|
||||
<?php
|
||||
|
||||
namespace Modules\Lpj\Http\Requests;
|
||||
|
||||
use Illuminate\Foundation\Http\FormRequest;
|
||||
|
||||
class ReferensiLinkRequest extends FormRequest
|
||||
{
|
||||
/**
|
||||
* Get the validation rules that apply to the request.
|
||||
*
|
||||
* @return array<string, mixed>
|
||||
*/
|
||||
public function rules(): array
|
||||
{
|
||||
$rules = [
|
||||
'name' => 'required|string|max:255',
|
||||
'link' => 'required|string|max:500',
|
||||
'kategori' => 'nullable|string|max:100',
|
||||
'deskripsi' => 'nullable|string|max:2000',
|
||||
'is_active' => 'boolean',
|
||||
'urutan' => 'nullable|integer|min:0',
|
||||
];
|
||||
|
||||
// Validasi tambahan untuk link
|
||||
$rules['link'] .= '|url';
|
||||
|
||||
return $rules;
|
||||
}
|
||||
|
||||
/**
|
||||
* Determine if the user is authorized to make this request.
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function authorize(): bool
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get custom messages for validator errors.
|
||||
*
|
||||
* @return array<string, string>
|
||||
*/
|
||||
public function messages(): array
|
||||
{
|
||||
return [
|
||||
'name.required' => 'Nama referensi link wajib diisi',
|
||||
'name.string' => 'Nama referensi link harus berupa teks',
|
||||
'name.max' => 'Nama referensi link maksimal 255 karakter',
|
||||
'link.required' => 'Link wajib diisi',
|
||||
'link.string' => 'Link harus berupa teks',
|
||||
'link.max' => 'Link maksimal 500 karakter',
|
||||
'link.url' => 'Link harus berupa URL yang valid',
|
||||
'kategori.string' => 'Kategori harus berupa teks',
|
||||
'kategori.max' => 'Kategori maksimal 100 karakter',
|
||||
'deskripsi.string' => 'Deskripsi harus berupa teks',
|
||||
'deskripsi.max' => 'Deskripsi maksimal 2000 karakter',
|
||||
'is_active.boolean' => 'Status aktif harus berupa ya/tidak',
|
||||
'urutan.integer' => 'Urutan harus berupa angka',
|
||||
'urutan.min' => 'Urutan minimal 0',
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Get custom attributes for validator errors.
|
||||
*
|
||||
* @return array<string, string>
|
||||
*/
|
||||
public function attributes(): array
|
||||
{
|
||||
return [
|
||||
'name' => 'Nama Referensi Link',
|
||||
'link' => 'Link',
|
||||
'kategori' => 'Kategori',
|
||||
'deskripsi' => 'Deskripsi',
|
||||
'is_active' => 'Status Aktif',
|
||||
'urutan' => 'Urutan',
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Prepare the data for validation.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
protected function prepareForValidation(): void
|
||||
{
|
||||
// Format link jika belum memiliki protocol
|
||||
if ($this->has('link')) {
|
||||
$link = $this->input('link');
|
||||
if ($link && !preg_match('/^(https?:\/\/)/i', $link)) {
|
||||
$this->merge([
|
||||
'link' => 'https://' . $link
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
// Set default is_active jika tidak diset
|
||||
if (!$this->has('is_active')) {
|
||||
$this->merge([
|
||||
'is_active' => true
|
||||
]);
|
||||
}
|
||||
|
||||
// Set default urutan jika tidak diset atau 0
|
||||
if (!$this->has('urutan') || empty($this->input('urutan'))) {
|
||||
$this->merge([
|
||||
'urutan' => 0
|
||||
]);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user