dress/app/Concerns/ProfileValidationRules.php

64 lines
1.8 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 [
'username' => $this->usernameRules($userId),
'email' => $this->emailRules($userId),
'nik' => ['required', 'string', 'size:16'],
'full_name' => ['required', 'string', 'max:100'],
'phone_number' => ['required', 'string', 'max:20'],
'address' => ['required', 'string'],
'birth_place' => ['required', 'string', 'max:100'],
'birth_date' => ['required', 'date'],
];
}
/**
* Get the validation rules used to validate usernames.
*
* @return array<int, \Illuminate\Contracts\Validation\Rule|array<mixed>|string>
*/
protected function usernameRules(?int $userId = null): array
{
return [
'required',
'string',
'max:20',
$userId === null
? Rule::unique(User::class)
: Rule::unique(User::class)->ignore($userId),
];
}
/**
* 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),
];
}
}