Use the session values.
Livewire : repopulate fields in form from `old()` within components
I have a Livewire component containing an input field which is part of a form. I am using Livewire to suggest input if it exists in the database which would populate other input fields within that component. The entire form is not Livewire.
When the form is submitted, it triggers the store method in the controller (as traditionally done), where validation is performed, and if validation fails it would send the user back to the form to show the the old data for example value="{{old('field1')}}"
How do we pass the old() input values and repopulate the Livewire component input field if validation fails on form submit? Some of the input fields are nested Livewire components.
If I understand correctly, upon validation failure your component receives the data back as an old() value and you would like to know how to repopulate the component with that. If this is the case see the following example on how to mount() the old() data when your page loads:
Lets say your view for your input would be something like:
<input type="text" name="inputData" wire:model.debounce.500ms="inputData">
Then your Component logic would be like so:
class SomeInputComponent extends Component
{
public $inputData;
public function render()
{
return view('livewire.add-source');
}
public function mount() {
if (old('inputData')) {
$this->inputData = old('inputData');
}
}
}
The mount function loads the old('inputData') into the component's $inputData which is then populated in the view linked by the wire:model.debounce.500ms="inputData".
I hope this helps.
Please or to participate in this conversation.