Die zwischen Benutzer-Anlegen und -Bearbeiten identischen Validierungsbloecke liegen jetzt in ProvidesUserFormRules: userIdentityRules(?int $ignore) (E-Mail- Unique ignoriert beim Bearbeiten den eigenen Datensatz), userRolesAndCompaniesRules() und userBillingRules(). Beide Formulare nutzen die Array-Spreads; die nur im Bearbeiten genutzten Profil-/Kontakt-Regeln bleiben dort inline. Ungenutzten Rule-Import aus dem Anlegen-Formular entfernt. Offen (mit User abzustimmen, UI-/Divergenz-lastig): gemeinsamer Save-Service und die Blade-Feld-Components. 31 User-Tests gruen. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
74 lines
2.7 KiB
PHP
74 lines
2.7 KiB
PHP
<?php
|
|
|
|
namespace App\Livewire\Concerns;
|
|
|
|
use App\Enums\Portal;
|
|
use App\Enums\RegistrationType;
|
|
use Illuminate\Validation\Rule;
|
|
|
|
/**
|
|
* Gemeinsame Validierungsregeln der Benutzer-Formulare (Anlegen/Bearbeiten).
|
|
* Nur die zwischen beiden Formularen identischen Blöcke leben hier; die rein
|
|
* im Bearbeiten genutzten Profil-/Kontakt-Regeln bleiben dort inline.
|
|
*/
|
|
trait ProvidesUserFormRules
|
|
{
|
|
/**
|
|
* Identitäts-Stammdaten. Beim Bearbeiten ignoriert die E-Mail-Unique-Regel
|
|
* den eigenen Datensatz.
|
|
*
|
|
* @return array<string, mixed>
|
|
*/
|
|
protected function userIdentityRules(?int $ignoreUserId = null): array
|
|
{
|
|
$emailUnique = Rule::unique('users', 'email');
|
|
|
|
if ($ignoreUserId !== null) {
|
|
$emailUnique = $emailUnique->ignore($ignoreUserId);
|
|
}
|
|
|
|
return [
|
|
'name' => ['required', 'string', 'max:255'],
|
|
'email' => ['required', 'email', $emailUnique],
|
|
'portal' => ['required', Rule::in(array_map(static fn (Portal $portal): string => $portal->value, Portal::cases()))],
|
|
'registrationType' => ['required', Rule::in(array_map(static fn (RegistrationType $type): string => $type->value, RegistrationType::cases()))],
|
|
'language' => ['required', 'string', Rule::in(['de', 'en'])],
|
|
];
|
|
}
|
|
|
|
/**
|
|
* Rollen-Auswahl plus Firmen-Verknüpfungen mit Pivot-Rolle.
|
|
*
|
|
* @return array<string, mixed>
|
|
*/
|
|
protected function userRolesAndCompaniesRules(): array
|
|
{
|
|
return [
|
|
'selectedRoles' => ['required', 'array', 'min:1'],
|
|
'selectedRoles.*' => ['string', Rule::exists('roles', 'name')],
|
|
'linkedCompanyIds' => ['array'],
|
|
'linkedCompanyIds.*' => ['integer', Rule::exists('companies', 'id')],
|
|
'companyRoles' => ['array'],
|
|
'companyRoles.*' => ['string', Rule::in(['member', 'responsible', 'owner'])],
|
|
];
|
|
}
|
|
|
|
/**
|
|
* Rechnungsadresse (optional; nur gespeichert, wenn vollständig).
|
|
*
|
|
* @return array<string, mixed>
|
|
*/
|
|
protected function userBillingRules(): array
|
|
{
|
|
return [
|
|
'billing.salutation_key' => ['nullable', 'string', 'max:20'],
|
|
'billing.title' => ['nullable', 'string', 'max:80'],
|
|
'billing.name' => ['nullable', 'string', 'max:255'],
|
|
'billing.address1' => ['nullable', 'string', 'max:255'],
|
|
'billing.address2' => ['nullable', 'string', 'max:255'],
|
|
'billing.postal_code' => ['nullable', 'string', 'max:20'],
|
|
'billing.city' => ['nullable', 'string', 'max:120'],
|
|
'billing.country_code' => ['nullable', 'string', 'size:2'],
|
|
];
|
|
}
|
|
}
|