1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace App\Http\Requests\Profile; |
4
|
|
|
|
5
|
|
|
use App\Enum\Gender; |
6
|
|
|
use App\Infrastructure\Foundation\Http\FormRequest; |
7
|
|
|
use App\Models\User; |
8
|
|
|
use Illuminate\Support\Arr; |
9
|
|
|
use Illuminate\Support\Facades\Auth; |
10
|
|
|
use Illuminate\Validation\Rule; |
11
|
|
|
use Illuminate\Validation\Rules; |
12
|
|
|
use Propaganistas\LaravelPhone\PhoneNumber; |
13
|
|
|
|
14
|
|
|
class UpdateRequest extends FormRequest |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* {@inheritDoc} |
18
|
|
|
*/ |
19
|
|
|
public function rules() |
20
|
|
|
{ |
21
|
|
|
/** @var \App\Models\User $user */ |
22
|
|
|
$user = Auth::user(); |
23
|
|
|
|
24
|
|
|
return [ |
25
|
|
|
'username' => ['required', 'string', 'max:255', Rule::unique(User::class)->ignoreModel($user)], |
26
|
|
|
'fullname' => 'required|string|max:255', |
27
|
|
|
'gender' => 'sometimes|nullable|enum:' . Gender::class, |
28
|
|
|
'email' => ['required', 'string', 'email', 'max:255', Rule::unique(User::class)->ignoreModel($user)], |
29
|
|
|
'phone_country' => 'sometimes|in:ID', |
30
|
|
|
'phone' => ['required', 'string', 'phone:ID', function ($attribute, $phone, $fail) use ($user) { |
31
|
|
|
$userCount = User::where($attribute, PhoneNumber::make($phone, request()->input('phone_country', env('PHONE_COUNTRY', 'ID')))->formatE164()) |
32
|
|
|
->where($user->getKeyName(), '!=', $user->getKey()) |
33
|
|
|
->count(); |
34
|
|
|
|
35
|
|
|
if ($userCount > 0) { |
36
|
|
|
$fail(trans('validation.unique', ['attribute' => static::getAttributes()[$attribute]])); |
37
|
|
|
} |
38
|
|
|
}], |
39
|
|
|
'password' => ['sometimes', 'nullable', 'confirmed', Rules\Password::defaults()], |
40
|
|
|
]; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* {@inheritDoc} |
45
|
|
|
*/ |
46
|
|
|
public function validated() |
47
|
|
|
{ |
48
|
|
|
$validated = parent::validated(); |
49
|
|
|
|
50
|
|
|
if ($this->isNotFilled('password')) { |
51
|
|
|
Arr::forget($validated, 'password'); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
return $validated; |
55
|
|
|
} |
56
|
|
|
} |
57
|
|
|
|