Update EditServer egg variables to match CreateServer

validation is still broken on `nullable|<##>` rules
This commit is contained in:
notCharles 2024-05-12 22:37:40 -04:00
parent 08dc5753d4
commit f9e8adad30

View File

@ -244,35 +244,48 @@ class EditServer extends EditRecord
]))
->schema([
Forms\Components\Repeater::make('server_variables')
->label('')
->relationship('serverVariables')
->grid()
->deletable(false)
->addable(false)
->schema([
Forms\Components\TextInput::make('variable_value')
->reorderable(false)->addable(false)->deletable(false)
->schema(function () {
$text = Forms\Components\TextInput::make('variable_value')
->hidden($this->shouldHideComponent(...))
->maxLength(191)
->rules([
fn (Forms\Get $get): Closure => function (string $attribute, $value, Closure $fail) use ($get) {
fn (ServerVariable $serverVariable): Closure => function (string $attribute, $value, Closure $fail) use ($serverVariable) {
$validator = Validator::make(['validatorkey' => $value], [
'validatorkey' => $get('rules'),
'validatorkey' => $serverVariable->variable->rules,
]);
if ($validator->fails()) {
$message = str($validator->errors()->first())->replace('validatorkey', $get('name'));
$message = str($validator->errors()->first())->replace('validatorkey', $serverVariable->variable->name);
$fail($message);
}
},
])
->label(fn (ServerVariable $variable) => $variable->variable->name)
->hintIcon('tabler-code')
->hintIconTooltip(fn (ServerVariable $variable) => $variable->variable->rules)
->prefix(fn (ServerVariable $variable) => '{{' . $variable->variable->env_variable . '}}')
->helperText(fn (ServerVariable $variable) => $variable->variable->description ?: '—')
->maxLength(191),
]);
Forms\Components\Hidden::make('variable_id'),
])
$select = Forms\Components\Select::make('variable_value')
->hidden($this->shouldHideComponent(...))
->options($this->getSelectOptionsFromRules(...))
->selectablePlaceholder(false);
$components = [$text, $select];
/** @var Forms\Components\Component $component */
foreach ($components as &$component) {
$component = $component
->live(onBlur: true)
->hintIcon('tabler-code')
->label(fn (ServerVariable $serverVariable) => $serverVariable->variable->name)
->hintIconTooltip(fn (ServerVariable $serverVariable) => $serverVariable->variable->rules)
->prefix(fn (ServerVariable $serverVariable) => '{{' . $serverVariable->variable->env_variable . '}}')
->helperText(fn (ServerVariable $serverVariable) => empty($serverVariable->variable->description) ? '—' : $serverVariable->variable->description);
}
return $components;
})
->columnSpan(2),
]),
@ -487,13 +500,6 @@ class EditServer extends EditRecord
->color('danger')
->after(fn (Server $server) => resolve(ServerDeletionService::class)->handle($server))
->requiresConfirmation(),
Actions\DeleteAction::make('Force Delete')
->label('Force Delete')
->hidden()
->successRedirectUrl(route('filament.admin.resources.servers.index'))
->color('danger')
->after(fn (Server $server) => resolve(ServerDeletionService::class)->withForce()->handle($server))
->requiresConfirmation(),
Actions\Action::make('console')
->label('Console')
->icon('tabler-terminal')
@ -520,4 +526,35 @@ class EditServer extends EditRecord
ServerResource\RelationManagers\AllocationsRelationManager::class,
];
}
private function shouldHideComponent(Forms\Get $get, Forms\Components\Component $component): bool
{
$containsRuleIn = str($get('rules'))->explode('|')->reduce(
fn ($result, $value) => $result === true && !str($value)->startsWith('in:'), true
);
if ($component instanceof Forms\Components\Select) {
return $containsRuleIn;
}
if ($component instanceof Forms\Components\TextInput) {
return !$containsRuleIn;
}
throw new \Exception('Component type not supported: ' . $component::class);
}
private function getSelectOptionsFromRules(Forms\Get $get): array
{
$inRule = str($get('rules'))->explode('|')->reduce(
fn ($result, $value) => str($value)->startsWith('in:') ? $value : $result, ''
);
return str($inRule)
->after('in:')
->explode(',')
->each(fn ($value) => str($value)->trim())
->mapWithKeys(fn ($value) => [$value => $value])
->all();
}
}