'required|string|max:255', 'domain' => 'required|string|max:255', 'cls' => ['required', 'string', 'max:255', Rule::in(app('mail-gateways'))], 'params' => 'present|array', ...$this->cls ? collect($this->cls::rules($this->id ? 'updateValidator' : 'storeValidator'))->mapWithKeys(fn ($rules, $key) => ["params.{$key}" => $rules]) : [], ]; } public function validationAttributes(): array { return [ 'cls' => 'Typ', 'name' => 'Beschreibung', 'domain' => 'Domain', ...$this->cls ? collect($this->cls::fieldNames())->mapWithKeys(fn ($attribute, $key) => ["params.{$key}" => $attribute]) : [], ]; } public function mount(?string $model = null): void { $this->types = app('mail-gateways')->map(fn ($gateway) => [ 'name' => $gateway::name(), 'id' => $gateway, ]); $model = Mailgateway::find($model); if ($model) { $this->id = $model->id; $this->name = $model->name; $this->domain = $model->domain; $this->cls = get_class($model->type); $this->params = (array) $model->type; } } public function updatedType(string $type): void { $this->params = $type::defaults(); } public function fields(): array { return $this->cls ? $this->cls::fields() : []; } #[On('onStoreFromModal')] public function onSave(): void { $this->validate(); if (!app($this->cls)->setParams($this->params)->works()) { throw ValidationException::withMessages(['connection' => 'Verbindung fehlgeschlagen.']); } $payload = [ 'name' => $this->name, 'domain' => $this->domain, 'type' => ['cls' => $this->cls, 'params' => $this->params], ]; if ($this->id) { Mailgateway::find($this->id)->update($payload); } else { Mailgateway::create($payload); } $this->dispatch('closeModal'); $this->dispatch('refresh'); $this->dispatch('success', 'Erfolgreich gespeichert.'); } public function render() { return <<<'HTML'