How to show old form value when validation fails?

The old function retrieves an old input value flashed into the session:

$value = old('value');

https://laravel.com/docs/8.x/helpers#method-old

How is this supposed to work in livewire? Say I have following method:

public function save(): void
{
    $this->validate();
    $this->entity->save();
}

If I examine the session in the debugbar, there are no form values being flashed when validation fails.

Hey, @artworkad

If you are talking about livewire use data binding and let it do the job for you.

Ex:

<input type="text" wire:model="name"/>
$this->validate([
'name' => 'required',
]);

I know about data binding. However when e.g. name is required and you submit it as empty, the validation fails but the input field is still empty. Livewire does not populate the model with the old value.

Can you provide an example code from your blade file?

You misunderstand the use of old(). It restores the previous submission, not what you had before you submitted the form.

In the case of Livewire, old() has no purpose since the value is bound from frontend to backend. If you want to revert to the value before the user changed it then you need to keep a copy of the original value or refetch it from its source.

1 Like

Livewire is so freaking cool :wink:

<div x-data="{ name: '{{$name}}' }">

:slightly_smiling_face::muscle:

Why are you not using the livewire way of validation? You don’t need to make a traditional post request.
Just read the docs: https://laravel-livewire.com/docs/2.x/input-validation