Enhance VM creation request validation and UI.
- Introduced `prepareForValidation` method to handle input merging for `behind_traefik` and `devices`. - Updated validation rules to conditionally require `subdomain` based on `behind_traefik`. - Added custom attribute names and error messages for better user feedback. - Improved the create VM form to dynamically show/hide subdomain fields based on the `behind_traefik` checkbox state. - Adjusted the user selection logic to display a message when no customers are available.
This commit is contained in:
@@ -13,14 +13,55 @@ class StoreVmRequest extends FormRequest
|
||||
return $this->user()->can('create', Customer::class);
|
||||
}
|
||||
|
||||
protected function prepareForValidation(): void
|
||||
{
|
||||
$behindTraefik = $this->resolveBehindTraefik();
|
||||
|
||||
$merge = ['behind_traefik' => $behindTraefik];
|
||||
|
||||
if (! $behindTraefik) {
|
||||
$merge['subdomain'] = null;
|
||||
}
|
||||
|
||||
if ($this->has('devices') && is_array($this->input('devices'))) {
|
||||
$devices = collect($this->input('devices'))
|
||||
->filter(fn ($device) => is_array($device) && ! empty($device['type']))
|
||||
->values()
|
||||
->all();
|
||||
|
||||
$merge['devices'] = $devices === [] ? null : $devices;
|
||||
}
|
||||
|
||||
$this->merge($merge);
|
||||
}
|
||||
|
||||
public function wantsTraefik(): bool
|
||||
{
|
||||
return $this->boolean('behind_traefik');
|
||||
}
|
||||
|
||||
private function resolveBehindTraefik(): bool
|
||||
{
|
||||
if (! $this->has('behind_traefik')) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$value = $this->input('behind_traefik');
|
||||
|
||||
if (is_array($value)) {
|
||||
return in_array('1', $value, true) || in_array(1, $value, true) || in_array(true, $value, true);
|
||||
}
|
||||
|
||||
return filter_var($value, FILTER_VALIDATE_BOOLEAN);
|
||||
}
|
||||
|
||||
public function rules(): array
|
||||
{
|
||||
$baseDomain = config('hosting.plesk.base_domain');
|
||||
|
||||
return [
|
||||
'name' => ['required', 'string', 'max:100', 'regex:/^[a-zA-Z0-9][a-zA-Z0-9_-]*$/'],
|
||||
'subdomain' => [
|
||||
Rule::requiredIf(fn () => $this->boolean('behind_traefik')),
|
||||
Rule::excludeIf(fn () => ! $this->wantsTraefik()),
|
||||
Rule::requiredIf(fn () => $this->wantsTraefik()),
|
||||
'nullable',
|
||||
'string',
|
||||
'max:63',
|
||||
@@ -28,7 +69,6 @@ class StoreVmRequest extends FormRequest
|
||||
],
|
||||
'behind_traefik' => ['boolean'],
|
||||
'user_id' => [
|
||||
Rule::requiredIf(fn () => $this->user()->isAdmin()),
|
||||
'nullable',
|
||||
'exists:users,id',
|
||||
],
|
||||
@@ -46,7 +86,7 @@ class StoreVmRequest extends FormRequest
|
||||
|
||||
public function domain(): ?string
|
||||
{
|
||||
if (! $this->boolean('behind_traefik') || ! $this->filled('subdomain')) {
|
||||
if (! $this->wantsTraefik() || ! $this->filled('subdomain')) {
|
||||
return null;
|
||||
}
|
||||
|
||||
@@ -61,4 +101,33 @@ class StoreVmRequest extends FormRequest
|
||||
|
||||
return $this->user()->id;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<string, string>
|
||||
*/
|
||||
public function attributes(): array
|
||||
{
|
||||
return [
|
||||
'name' => 'VM-Name',
|
||||
'subdomain' => 'Subdomain',
|
||||
'user_id' => 'Kunde',
|
||||
'cpu' => 'vCPUs',
|
||||
'ram' => 'RAM',
|
||||
'disk' => 'Festplatte',
|
||||
'install_iso' => 'Installations-ISO',
|
||||
'devices.*.type' => 'Gerätetyp',
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<string, string>
|
||||
*/
|
||||
public function messages(): array
|
||||
{
|
||||
return [
|
||||
'required' => ':attribute ist erforderlich.',
|
||||
'regex' => ':attribute hat ein ungültiges Format.',
|
||||
'exists' => 'Der gewählte :attribute ist ungültig.',
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
28
lang/de/validation.php
Normal file
28
lang/de/validation.php
Normal file
@@ -0,0 +1,28 @@
|
||||
<?php
|
||||
|
||||
return [
|
||||
|
||||
'accepted' => ':attribute muss akzeptiert werden.',
|
||||
'required' => ':attribute muss ausgefüllt werden.',
|
||||
'string' => ':attribute muss eine Zeichenkette sein.',
|
||||
'integer' => ':attribute muss eine ganze Zahl sein.',
|
||||
'boolean' => ':attribute muss wahr oder falsch sein.',
|
||||
'email' => ':attribute muss eine gültige E-Mail-Adresse sein.',
|
||||
'max' => [
|
||||
'string' => ':attribute darf maximal :max Zeichen haben.',
|
||||
'integer' => ':attribute darf maximal :max sein.',
|
||||
],
|
||||
'min' => [
|
||||
'string' => ':attribute muss mindestens :min Zeichen haben.',
|
||||
'integer' => ':attribute muss mindestens :min sein.',
|
||||
],
|
||||
'regex' => 'Das Format von :attribute ist ungültig.',
|
||||
'unique' => ':attribute ist bereits vergeben.',
|
||||
'confirmed' => ':attribute stimmt nicht mit der Bestätigung überein.',
|
||||
'exists' => 'Der gewählte :attribute ist ungültig.',
|
||||
'in' => 'Der gewählte :attribute ist ungültig.',
|
||||
'required_if' => ':attribute ist erforderlich, wenn :other :value ist.',
|
||||
|
||||
'attributes' => [],
|
||||
|
||||
];
|
||||
@@ -13,30 +13,39 @@
|
||||
<input name="name" value="{{ old('name') }}" required class="mt-1 w-full rounded-lg border border-slate-700 bg-slate-800 px-3 py-2">
|
||||
</label>
|
||||
|
||||
@if($customers->isNotEmpty())
|
||||
@if(auth()->user()->isAdmin())
|
||||
<label class="block">
|
||||
<span class="text-sm text-slate-400">Zugewiesener Kunde</span>
|
||||
<select name="user_id" required class="mt-1 w-full rounded-lg border border-slate-700 bg-slate-800 px-3 py-2">
|
||||
<span class="text-sm text-slate-400">Zugewiesener Benutzer</span>
|
||||
<select name="user_id" class="mt-1 w-full rounded-lg border border-slate-700 bg-slate-800 px-3 py-2">
|
||||
<option value="">— Mir selbst (Admin) —</option>
|
||||
@foreach($customers as $c)
|
||||
<option value="{{ $c->id }}" @selected(old('user_id') == $c->id)>{{ $c->name }} ({{ $c->email }})</option>
|
||||
@endforeach
|
||||
</select>
|
||||
@if($customers->isEmpty())
|
||||
<p class="mt-1 text-xs text-slate-500">Noch keine Kundenkonten – VM wird deinem Admin-Konto zugeordnet. Kunden unter „Benutzer“ anlegen.</p>
|
||||
@endif
|
||||
</label>
|
||||
@endif
|
||||
|
||||
@php
|
||||
$behindTraefik = filter_var(old('behind_traefik', '1'), FILTER_VALIDATE_BOOLEAN);
|
||||
@endphp
|
||||
<label class="flex items-center gap-2">
|
||||
<input type="hidden" name="behind_traefik" value="0">
|
||||
<input type="checkbox" name="behind_traefik" value="1" @checked(old('behind_traefik', true)) id="behind_traefik" class="rounded border-slate-600 text-cyan-500">
|
||||
<input type="checkbox" name="behind_traefik" value="1" @checked($behindTraefik) id="behind_traefik" class="rounded border-slate-600 text-cyan-500">
|
||||
<span class="text-sm">Hinter Traefik (Subdomain + DNS)</span>
|
||||
</label>
|
||||
|
||||
<div id="traefik-fields">
|
||||
<div id="traefik-fields" @if(!$behindTraefik) style="display:none" @endif>
|
||||
<label class="block">
|
||||
<span class="text-sm text-slate-400">Subdomain</span>
|
||||
<span class="text-sm text-slate-400">Subdomain <span class="text-red-400">*</span></span>
|
||||
<div class="mt-1 flex">
|
||||
<input name="subdomain" value="{{ old('subdomain') }}" class="w-full rounded-l-lg border border-slate-700 bg-slate-800 px-3 py-2">
|
||||
<input name="subdomain" id="subdomain" value="{{ old('subdomain') }}" placeholder="meine-vm"
|
||||
class="w-full rounded-l-lg border border-slate-700 bg-slate-800 px-3 py-2 @error('subdomain') border-red-500 @enderror">
|
||||
<span class="flex items-center rounded-r-lg border border-l-0 border-slate-700 bg-slate-800 px-3 text-sm text-slate-500">.{{ config('hosting.plesk.base_domain') }}</span>
|
||||
</div>
|
||||
@error('subdomain')<p class="mt-1 text-xs text-red-400">{{ $message }}</p>@enderror
|
||||
<p class="mt-1 text-xs text-slate-500">Pflichtfeld bei „Hinter Traefik“ (nur a-z, 0-9, Bindestrich).</p>
|
||||
</label>
|
||||
</div>
|
||||
|
||||
@@ -90,7 +99,12 @@
|
||||
<script>
|
||||
const cb = document.getElementById('behind_traefik');
|
||||
const fields = document.getElementById('traefik-fields');
|
||||
function toggle() { fields.style.display = cb.checked ? 'block' : 'none'; }
|
||||
const subdomain = document.getElementById('subdomain');
|
||||
function toggle() {
|
||||
const on = cb.checked;
|
||||
fields.style.display = on ? 'block' : 'none';
|
||||
if (subdomain) subdomain.required = on;
|
||||
}
|
||||
cb?.addEventListener('change', toggle);
|
||||
toggle();
|
||||
</script>
|
||||
|
||||
Reference in New Issue
Block a user