Volt, Bug $this->reset() i have this:
<?php
use function Livewire\Volt\{state};
state([
'name' => 'default name :)',
'anyint' => 10,
]);
$resetValues = function () {
$this->reset();
}; ?>
<div>
<input type="text" wire:model="name" />
<input type="number" wire:model="anyint" />
<button class="rounded-md bg-red-300 p-4" wire:click="resetValues">
reset values to default
</button>
</div>
the initial value is loaded correctly for the inputs, but if I reset using this->reset all values that exist in state become null.
while if we use:
<?php
use Livewire\Volt\Component;
new class extends Component {
public $name = 'default name :)';
public $anyint = 10;
public function resetValues()
{
$this->reset();
}
}; ?>
<div>
<input type="text" wire:model="name" />
<input type="number" wire:model="anyint" />
<button class="rounded-md bg-red-300 p-4" wire:click="resetValues">
reset values to default
</button>
</div>
it works correctly, is that right? using this->reset with state doesn't work?
It has to do with how Livewire handles default property values in a class based component vs a function based volt component.
In your case you need to reset the values in resetValues function
<?php
use function Livewire\Volt\{state,boot};
state([
'name' => 'default name :)',
'anyint' => 10,
]);
$resetValues = function () {
$this->reset();
$this->name = 'default name :)';
$this->anyint = 10;
};
?>
<div class="inline-flex items-center space-x-3">
<flux:input type="text" wire:model="name" size="sm"/>
<flux:input type="number" wire:model="anyint" size="sm"/>
<flux:button type="button" wire:click="resetValues" size="sm">
Reset
</flux:button>
</div>
@RobertsP I understand, but what I wanted to know, if it was really supposed to be that way, when using reset it resets everything to null instead of returning to the default, is this the expected behavior?
Please sign in or create an account to participate in this conversation.