Custom Field that Extends TextInput etc to AutoSave

I am trying somehow to create a parameter i can pass to a textinput that autosaves a field this is my customclass
<?php
namespace App\Forms\Components;

use Filament\Forms\Components\Field;
use Filament\Forms\Components\TextInput;
use Filament\Forms\Contracts\HasForms;
use Illuminate\Database\Eloquent\Model;

class AutoSaveTextInput extends TextInput
{
// ...

protected bool $autoSave = false;

public function autoSave(bool $value = true): static
{
$this->autoSave = $value;

return $this;
}

public static function handleAfterStateUpdated(Field $component, HasForms $livewire, ?Model $record, mixed $state)
{
if (! $record || ! $component->autoSave) {
return; // If there is nowhere to save or autoSave is false, return.
}

$livewire->validateOnly($component->getStatePath());
$record->update([$component->getName() => $state]);
}
}
<?php
namespace App\Forms\Components;

use Filament\Forms\Components\Field;
use Filament\Forms\Components\TextInput;
use Filament\Forms\Contracts\HasForms;
use Illuminate\Database\Eloquent\Model;

class AutoSaveTextInput extends TextInput
{
// ...

protected bool $autoSave = false;

public function autoSave(bool $value = true): static
{
$this->autoSave = $value;

return $this;
}

public static function handleAfterStateUpdated(Field $component, HasForms $livewire, ?Model $record, mixed $state)
{
if (! $record || ! $component->autoSave) {
return; // If there is nowhere to save or autoSave is false, return.
}

$livewire->validateOnly($component->getStatePath());
$record->update([$component->getName() => $state]);
}
}
I want to add the parameter ->autosave(true) to the field - Something like this
AutoSaveTextInput::make('name')->autofocus()->required()

->columnSpan(6)
->prefix('Task Name*')
->required()
->label('')
->reactive()
->live(true)
->autoSave(true),
AutoSaveTextInput::make('name')->autofocus()->required()

->columnSpan(6)
->prefix('Task Name*')
->required()
->label('')
->reactive()
->live(true)
->autoSave(true),
Any hints on how i can use the afterStateUpdated to call this function? Thanks in advance!!
Solution:
🙂 ```php $this->afterStateUpdated(fn( Field $component, HasForms $livewire,...
Jump to solution
7 Replies
Lara Zeus
Lara Zeus13mo ago
you can do it in setUp function
protected function setUp(): void
{
parent::setUp();

$this->afterStateUpdated(function(){
dd(9);
});
}
protected function setUp(): void
{
parent::setUp();

$this->afterStateUpdated(function(){
dd(9);
});
}
nostrodamned
nostrodamnedOP13mo ago
sorry not quite sure what you mean? this goes on the clasS?
Lara Zeus
Lara Zeus13mo ago
yes this goes into the AutoSaveTextInput
nostrodamned
nostrodamnedOP13mo ago
Ahhh I see what you are up to 😉 one sec
Solution
Lara Zeus
Lara Zeus13mo ago
🙂
$this->afterStateUpdated(fn(
Field $component,
HasForms $livewire,
?Model $record,
mixed $state
) => $this->handleAfterStateUpdated($component, $livewire, $record, $state));
$this->afterStateUpdated(fn(
Field $component,
HasForms $livewire,
?Model $record,
mixed $state
) => $this->handleAfterStateUpdated($component, $livewire, $record, $state));
nostrodamned
nostrodamnedOP13mo ago
bloomin awesome! For anyone else this was my final code (probably needs other stuff in it!)
<?php
namespace App\Forms\Components;

use Filament\Forms\Components\Field;
use Filament\Forms\Components\TextInput;
use Filament\Forms\Contracts\HasForms;
use Illuminate\Database\Eloquent\Model;

class AutoSaveTextInput extends TextInput
{

protected bool $autoSave = false;

public function autoSave(bool $value = true): static
{
$this->autoSave = $value;

return $this;
}
public static function handleAfterStateUpdated(Field $component, HasForms $livewire, ?Model $record, mixed $state)
{
if (! $record || ! $component->autoSave) {
return; // If there is nowhere to save or autoSave is false, return.
}

$livewire->validateOnly($component->getStatePath());
$record->update([$component->getName() => $state]);
}
protected function setUp(): void
{
parent::setUp();
$this->afterStateUpdated(fn(
Field $component,
HasForms $livewire,
?Model $record,
mixed $state
) => $this->handleAfterStateUpdated($component, $livewire, $record, $state));
}

}
<?php
namespace App\Forms\Components;

use Filament\Forms\Components\Field;
use Filament\Forms\Components\TextInput;
use Filament\Forms\Contracts\HasForms;
use Illuminate\Database\Eloquent\Model;

class AutoSaveTextInput extends TextInput
{

protected bool $autoSave = false;

public function autoSave(bool $value = true): static
{
$this->autoSave = $value;

return $this;
}
public static function handleAfterStateUpdated(Field $component, HasForms $livewire, ?Model $record, mixed $state)
{
if (! $record || ! $component->autoSave) {
return; // If there is nowhere to save or autoSave is false, return.
}

$livewire->validateOnly($component->getStatePath());
$record->update([$component->getName() => $state]);
}
protected function setUp(): void
{
parent::setUp();
$this->afterStateUpdated(fn(
Field $component,
HasForms $livewire,
?Model $record,
mixed $state
) => $this->handleAfterStateUpdated($component, $livewire, $record, $state));
}

}
Now i can use
AutoSaveTextInput::make('name')->autofocus()->required()
->columnSpan(6)
->prefix('Task Name*')
->required()
->label('')
->reactive()
->live(true)
->autoSave(true),
AutoSaveTextInput::make('name')->autofocus()->required()
->columnSpan(6)
->prefix('Task Name*')
->required()
->label('')
->reactive()
->live(true)
->autoSave(true),
Thanks so much @Lara Zeus
Lara Zeus
Lara Zeus13mo ago
amazing 🙂
Want results from more Discord servers?
Add your server