63 lines
2.5 KiB
PHP
63 lines
2.5 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Requests\Users;
|
|
|
|
use App\Http\Requests\ApiFormRequest;
|
|
use App\Models\User;
|
|
use Illuminate\Validation\Rule;
|
|
|
|
class UserUpdateRequest extends ApiFormRequest
|
|
{
|
|
public function authorize(): bool
|
|
{
|
|
return true;
|
|
}
|
|
|
|
public function rules(): array
|
|
{
|
|
$userId = (int) (
|
|
$this->route('userId')
|
|
?? $this->route('id')
|
|
?? $this->input('id')
|
|
?? 0
|
|
);
|
|
$currentEmail = '';
|
|
|
|
if ($userId > 0) {
|
|
$currentEmail = strtolower(trim((string) (User::query()->whereKey($userId)->value('email') ?? '')));
|
|
}
|
|
|
|
$emailRules = ['sometimes', 'email', 'max:255'];
|
|
$incomingEmail = strtolower(trim((string) $this->input('email', '')));
|
|
if ($incomingEmail !== '' && $incomingEmail !== $currentEmail) {
|
|
$emailRules[] = Rule::unique('users', 'email')->ignore($userId);
|
|
}
|
|
|
|
return [
|
|
'firstname' => ['sometimes', 'string', 'max:255'],
|
|
'lastname' => ['sometimes', 'string', 'max:255'],
|
|
'gender' => ['sometimes', 'nullable', 'string', Rule::in(['Male', 'Female', 'male', 'female', 'MALE', 'FEMALE'])],
|
|
'cellphone' => ['sometimes', 'string', 'max:25'],
|
|
'email' => $emailRules,
|
|
'address_street' => ['sometimes', 'string', 'max:255'],
|
|
'apt' => ['sometimes', 'nullable', 'string', 'max:25'],
|
|
'city' => ['sometimes', 'string', 'max:255'],
|
|
'state' => ['sometimes', 'string', 'max:25'],
|
|
'zip' => ['sometimes', 'string', 'max:25'],
|
|
'accept_school_policy' => ['sometimes', 'boolean'],
|
|
'semester' => ['sometimes', 'string', 'max:255'],
|
|
'school_year' => ['sometimes', 'nullable', 'string', 'max:9'],
|
|
'school_id' => ['sometimes', 'nullable', 'integer'],
|
|
'user_type' => ['sometimes', 'nullable', 'string', Rule::in(['primary', 'secondary', 'tertiary'])],
|
|
'rfid_tag' => ['sometimes', 'nullable', 'string', 'max:100'],
|
|
'status' => ['sometimes', 'string', Rule::in(['Active', 'Inactive'])],
|
|
'is_verified' => ['sometimes', 'boolean'],
|
|
'is_suspended' => ['sometimes', 'boolean'],
|
|
'failed_attempts' => ['sometimes', 'nullable', 'integer'],
|
|
'last_failed_at' => ['sometimes', 'nullable', 'date'],
|
|
'token' => ['sometimes', 'nullable', 'string', 'max:255'],
|
|
'account_id' => ['sometimes', 'nullable', 'string', 'max:255'],
|
|
];
|
|
}
|
|
}
|