63 lines
1.9 KiB
PHP
63 lines
1.9 KiB
PHP
<?php
|
|
|
|
namespace App\Concerns;
|
|
|
|
use App\Models\User;
|
|
use Illuminate\Validation\Rule;
|
|
|
|
trait ProfileValidationRules
|
|
{
|
|
/**
|
|
* Get the validation rules used to validate user profiles.
|
|
*
|
|
* @return array<string, array<int, \Illuminate\Contracts\Validation\Rule|array<mixed>|string>>
|
|
*/
|
|
protected function profileRules(?int $userId = null): array
|
|
{
|
|
return [
|
|
'name' => $this->nameRules(),
|
|
'email' => $this->emailRules($userId),
|
|
// Optional extended profile fields
|
|
'first_name' => ['nullable','string','max:60'],
|
|
'last_name' => ['nullable','string','max:60'],
|
|
'gender' => ['nullable','string','max:24'],
|
|
'birthdate' => ['nullable','date','before:today'],
|
|
'country' => ['nullable','string','size:2'],
|
|
'address_line1' => ['nullable','string','max:120'],
|
|
'address_line2' => ['nullable','string','max:120'],
|
|
'city' => ['nullable','string','max:80'],
|
|
'state' => ['nullable','string','max:80'],
|
|
'postal_code' => ['nullable','string','max:32'],
|
|
'phone' => ['nullable','string','max:32'],
|
|
];
|
|
}
|
|
|
|
/**
|
|
* Get the validation rules used to validate user names.
|
|
*
|
|
* @return array<int, \Illuminate\Contracts\Validation\Rule|array<mixed>|string>
|
|
*/
|
|
protected function nameRules(): array
|
|
{
|
|
return ['required', 'string', 'max:255'];
|
|
}
|
|
|
|
/**
|
|
* Get the validation rules used to validate user emails.
|
|
*
|
|
* @return array<int, \Illuminate\Contracts\Validation\Rule|array<mixed>|string>
|
|
*/
|
|
protected function emailRules(?int $userId = null): array
|
|
{
|
|
return [
|
|
'required',
|
|
'string',
|
|
'email',
|
|
'max:255',
|
|
$userId === null
|
|
? Rule::unique(User::class)
|
|
: Rule::unique(User::class)->ignore($userId),
|
|
];
|
|
}
|
|
}
|