Table of Contents
📖 2 minutes read
Sometimes UI state and database state get out of sync – especially with single-option dropdowns in Vue.js where the UI shows a selected value but v-model is actually null. Instead of forcing users to manually fix this, detect and auto-correct it in the backend.
The Scenario
A form dropdown shows one option (e.g., “Production API”), it appears selected visually, but because there’s no “–Select One–” empty option, the v-model is still null. The form submits, and the backend receives null for a required field.
Backend Auto-Fix
public function forRecord(Record $record)
{
// Auto-assign when UI shows single option but v-model is null
if ($record->getConfig()?->getKey() === null && is_string($record->api_plugin)) {
$configs = app(ConfigRepository::class)->configsFor($record->api_plugin);
$hasOnlyOneConfig = $configs->containsOneItem();
$pluginSupportsClients = method_exists($record->api_plugin, 'getClients');
$allClientsUseDynamicConfigs = $pluginSupportsClients
&& app($record->api_plugin)->getClients()
->contains(fn (Client $client) => !$client->getConfig() instanceof DynamicConfig) === false;
if ($hasOnlyOneConfig && $pluginSupportsClients && $allClientsUseDynamicConfigs) {
$record->setConfig($configs->first());
}
}
$dynamic = $record->configs->unique()?->first();
return $dynamic ? new DynamicConfig($dynamic) : $this->getDefault();
}
When to Use This Pattern
- Only one logical choice exists
- UI appears to show it as selected
- Missing value causes errors downstream
- You want graceful degradation instead of hard failures
When NOT to Use
- Multiple valid options exist
- Selection is genuinely optional
- You want strict validation
This pattern mimics the UI’s apparent behavior in the backend, making the system more forgiving of common UI/UX patterns.
Leave a Reply