composer require mrbohem/livewire-form-keeper
</body>):
<x-livewire-form-keeper::script />
FormKeeper trait to your Livewire component:
use MrBohem\LivewireFormKeeper\FormKeeper;
class MyForm extends Component
{
use FormKeeper;
// Your form properties here
}
Component Integration:
FormKeeper trait to any Livewire component managing form data.Form Field Handling:
public $name = '';
public $email = '';
Explicit Control:
formKeeperProperties array to specify which properties to persist:
protected $formKeeperProperties = ['name', 'email']; // Only these will be kept
protected $formKeeperExclude = ['password', 'tempField'];
Manual Persistence:
public function saveFormState()
{
$this->persistFormState();
}
Integration with Navigation:
wire:navigate or Alpine.js for seamless transitions:
<button wire:navigate="{{ route('other.page') }}" @click="persistFormState">
Go to Next Page
</button>
Multi-Step Forms:
FormKeeper across multiple components in a multi-step workflow:
// Step 1 Component
public $step1Data = [];
use FormKeeper;
// Step 2 Component
public $step2Data = [];
use FormKeeper;
Custom Storage Backend:
public function getFormKeeperStorage()
{
return session()->get('form_keeper_data', []);
}
TTL (Time-to-Live):
public function getFormKeeperTTL()
{
return 3600; // 1 hour in seconds
}
Event-Based Persistence:
protected $listeners = ['formStateSaved' => 'handleFormStateSaved'];
public function handleFormStateSaved()
{
// Custom logic after persistence
}
Conditional Persistence:
public function shouldPersistFormState()
{
return $this->isFormValid();
}
Session Dependency:
sessionStorage by default. Ensure users aren’t in private/incognito mode where sessionStorage may behave unexpectedly.localStorage for broader compatibility (customize via the package’s config or override storage backend).Property Naming Conflicts:
formKeeperProperties or formKeeperExclude unless intentional, as they override the package’s defaults._formKeeperProperties).Large Forms:
sessionStorage or cause serialization issues.formKeeperExclude to omit non-critical data or implement client-side compression.Livewire Hydration:
wire:model.live, rapid updates may trigger unnecessary persistence. Debounce or throttle updates:
<input wire:model.debounce.500ms="name">
Multi-Tab Scenarios:
sessionStorage is tab-isolated. Data won’t sync across tabs by default.localStorage for cross-tab persistence or implement a server-side sync layer.CSRF Token Issues:
formKeeperProperties or reset them post-persistence.Inspect Storage:
Application > sessionStorage or localStorage).livewire-form-keeper-{component-hash}.Log Persisted Data:
public function debugFormKeeper()
{
dd($this->getFormKeeperStorage());
}
Clear Storage Manually:
// Run in browser console
sessionStorage.clear();
Check for Overrides:
sessionStorage or Livewire’s hydration.Custom Storage Key:
public function getFormKeeperKey()
{
return 'custom-form-keeper-' . $this->getId();
}
Serialization Control:
protected function serializeFormKeeperData($data)
{
return json_encode($data, JSON_UNESCAPED_UNICODE);
}
protected function deserializeFormKeeperData($data)
{
return json_decode($data, true);
}
Event Hooks:
public function beforePersistFormState()
{
// Pre-persistence logic
}
public function afterRestoreFormState()
{
// Post-restoration logic
}
Conditional Restoration:
public function shouldRestoreFormState()
{
return !$this->isFreshInstall() && auth()->check();
}
Fallback to Defaults:
public function getFormKeeperDefaults()
{
return [
'name' => 'Default Name',
'email' => 'default@example.com',
];
}
How can I help you explore Laravel packages today?