1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace App\Http\Requests; |
4
|
|
|
|
5
|
|
|
use Illuminate\Foundation\Http\FormRequest; |
6
|
|
|
use Illuminate\Support\Collection; |
7
|
|
|
use Illuminate\Validation\Rule; |
8
|
|
|
use LaravelEnso\People\Http\Requests\ValidatePersonRequest as EnsoPersonRequest; |
9
|
|
|
|
10
|
|
|
class ValidatePersonRequest extends EnsoPersonRequest |
11
|
|
|
{ |
12
|
|
|
private Collection $companies; |
|
|
|
|
13
|
|
|
|
14
|
|
|
public function authorize() |
15
|
|
|
{ |
16
|
|
|
return $this->emailUnchagedForUser(); |
17
|
|
|
} |
18
|
|
|
|
19
|
|
|
public function rules() |
20
|
|
|
{ |
21
|
|
|
return [ |
22
|
|
|
'title' => 'integer|nullable', |
23
|
|
|
'givn' => 'required|max:100', |
24
|
|
|
'surn' => 'string|max:100|nullable', |
25
|
|
|
'uid' => ['string', 'nullable', $this->unique('uid')], |
26
|
|
|
'email' => ['email', 'nullable', $this->unique('email')], |
27
|
|
|
'phone' => 'max:30|nullable', |
28
|
|
|
'birthday' => 'nullable|date', |
29
|
|
|
'deathday' => 'nullable|date', |
30
|
|
|
'position' => 'integer|nullable', |
31
|
|
|
'obs' => 'string|nullable', |
32
|
|
|
'companies' => 'array', |
33
|
|
|
'companies.*' => 'exists:companies,id', |
34
|
|
|
'company' => 'nullable|exists:companies,id|in:'.implode(',', $this->get('companies')), |
|
|
|
|
35
|
|
|
]; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
protected function unique(string $attribute) |
39
|
|
|
{ |
40
|
|
|
return Rule::unique('people', $attribute) |
41
|
|
|
->ignore(optional($this->route('person'))->id); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
private function emailUnchagedForUser() |
45
|
|
|
{ |
46
|
|
|
return ! optional($this->route('person'))->hasUser() |
47
|
|
|
|| $this->get('email') === $this->route('person')->email; |
48
|
|
|
} |
49
|
|
|
} |
50
|
|
|
|