first init
This commit is contained in:
@@ -0,0 +1,110 @@
|
||||
<?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',
|
||||
];
|
||||
}
|
||||
|
||||
// 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' => 'required|string|max:255',
|
||||
'phone_number' => 'nullable|string|max:255',
|
||||
'image_url' => 'nullable|string|max:255',
|
||||
'status' => 'nullable|string',
|
||||
];
|
||||
}
|
||||
|
||||
// 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',
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* 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.',
|
||||
];
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user