Files
My-KOPKB/be/Modules/User/Http/Requests/UserRequest.php
T

146 lines
6.1 KiB
PHP

<?php
namespace Modules\User\Http\Requests;
use Illuminate\Foundation\Http\FormRequest;
class UserRequest extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*/
public function authorize(): bool
{
return true;
}
/**
* Get the validation rules that apply to the request.
*
* @return array<string, \Illuminate\Contracts\Validation\ValidationRule|array<mixed>|string>
*/
public function rules(): array
{
// Get the user ID from the route (with null check)
$userId = $this->route() ? $this->route('user') : null;
// Check if this is a create or update operation
$isCreate = $this->isMethod('POST');
$isUpdate = $this->isMethod('PUT') || $this->isMethod('PATCH');
// If this is not a POST, PUT, or PATCH request, return minimal rules
if (! $isCreate && ! $isUpdate) {
return [
'name' => 'nullable|string|max:255',
'email' => 'nullable|string|email|max:255',
'password' => 'nullable|string|min:8',
'ic_number' => 'nullable|string|max:255',
'position' => 'nullable|string|max:255',
'phone_number' => 'nullable|string|max:255',
'image_url' => 'nullable|string|max:255',
'status' => 'nullable|string',
'gender' => 'nullable|string|max:255',
'marriage_status' => 'nullable|string|max:255',
'member_number' => 'required|integer',
'member_type' => 'required|string|max:255',
'join_date' => 'nullable|date',
'birth_date' => 'nullable|date',
'birth_place' => 'nullable|string|max:255',
];
}
// Build unique email validation rule
$emailUniqueRule = $userId ? 'unique:users,email,'.$userId : 'unique:users,email';
// If this is an update operation, make email and password optional
if ($isUpdate) {
return [
'name' => 'required|string|max:255',
// 'email' => [
// 'nullable',
// 'string',
// 'email',
// 'max:255',
// $emailUniqueRule,
// ],
'password' => 'nullable|string|min:8',
'ic_number' => 'required|string|max:255',
'position' => 'nullable|string|max:255',
'phone_number' => 'nullable|string|max:255',
'image_url' => 'nullable|string|max:255',
'status' => 'nullable|string',
'gender' => 'nullable|string|max:255',
'marriage_status' => 'nullable|string|max:255',
'member_number' => 'required|integer',
'member_type' => 'required|string|max:255',
'join_date' => 'nullable|date',
'birth_date' => 'nullable|date',
'birth_place' => 'nullable|string|max:255',
];
}
// Default rules for create operations
return [
'name' => 'required|string|max:255',
'email' => [
'required',
'string',
'email',
'max:255',
$emailUniqueRule,
],
'password' => 'nullable|string|min:8',
'ic_number' => 'required|string|max:255',
'position' => 'required|string|max:255',
'phone_number' => 'nullable|string|max:255',
'image_url' => 'nullable|string|max:255',
'status' => 'required|string',
'gender' => 'required|string|max:255',
'marriage_status' => 'required|string|max:255',
'member_number' => 'required|integer',
'member_type' => 'required|string|max:255',
'join_date' => 'required|date',
'birth_date' => 'required|date',
'birth_place' => 'required|string|max:255',
];
}
/**
* Get custom messages for validator errors.
*/
public function messages(): array
{
return [
'name.required' => 'Nama diperlukan.',
'name.max' => 'Nama tidak boleh melebihi 255 aksara.',
'email.required' => 'Email diperlukan.',
'email.email' => 'Email tidak sah.',
'email.max' => 'Email tidak boleh melebihi 255 aksara.',
'email.unique' => 'Email sudah wujud.',
'password.nullable' => 'Kata laluan diperlukan.',
'ic_number.required' => 'Nombor Kad Pengenalan diperlukan.',
'ic_number.max' => 'Nombor Kad Pengenalan tidak boleh melebihi 255 aksara.',
'position.required' => 'Posisi diperlukan.',
'position.max' => 'Posisi tidak boleh melebihi 255 aksara.',
'phone_number.max' => 'Nombor telefon tidak boleh melebihi 255 aksara.',
'image_url.max' => 'URL imej tidak boleh melebihi 255 aksara.',
'status.required' => 'Status diperlukan.',
'status.string' => 'Status aktif tidak sah.',
'gender.required' => 'Jenis kelamin diperlukan.',
'gender.max' => 'Jenis kelamin tidak boleh melebihi 255 aksara.',
'marriage_status.required' => 'Status perkahwinan diperlukan.',
'marriage_status.max' => 'Status perkahwinan tidak boleh melebihi 255 aksara.',
'member_number.required' => 'Nombor anggota diperlukan.',
'member_number.integer' => 'Nombor anggota tidak sah.',
'member_type.required' => 'Jenis anggota diperlukan.',
'member_type.max' => 'Jenis anggota tidak boleh melebihi 255 aksara.',
'join_date.required' => 'Tarikh join diperlukan.',
'join_date.date' => 'Tarikh join tidak sah.',
'birth_date.required' => 'Tarikh lahir diperlukan.',
'birth_date.date' => 'Tarikh lahir tidak sah.',
'birth_place.required' => 'Tempat lahir diperlukan.',
'birth_place.max' => 'Tempat lahir tidak boleh melebihi 255 aksara.',
];
}
}