| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152 |
- <?php
-
- namespace App\Concerns;
-
- use App\Models\User;
- use Illuminate\Contracts\Validation\ValidationRule;
- use Illuminate\Validation\Rule;
- use Illuminate\Validation\Rules\Unique;
-
- trait ProfileValidationRules
- {
- /**
- * Get the validation rules used to validate user profiles.
- *
- * @return array<string, array<int, Unique|ValidationRule|array<mixed>|string>>
- */
- protected function profileRules(?int $userId = null): array
- {
- return [
- 'name' => $this->nameRules(),
- 'email' => $this->emailRules($userId),
- ];
- }
-
- /**
- * Get the validation rules used to validate user names.
- *
- * @return array<int, Unique|ValidationRule|array<mixed>|string>
- */
- protected function nameRules(): array
- {
- return ['required', 'string', 'max:255'];
- }
-
- /**
- * Get the validation rules used to validate user emails.
- *
- * @return array<int, Unique|ValidationRule|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),
- ];
- }
- }
|