F
Filament13mo ago
tjodalv

How to update form field from resource Edit page's method?

I have resource edit form. In one placeholder field I am including my custom Livewire component. My Livewire component dispatches event 'update-stock' and with event it is sending $productId and $stock info. I am listening for that event on my resource edit page and when method is triggered I want to update stock field in the underlying form. But for some reason that stock variable doesn't change. This is code from my resource edit page:
// app/Filament/Resources/TaskResource/Pages/EditTask.php
...
protected $listeners = ['update-stock' => 'updateProductStock'];

public function updateProductStock($args)
{
$productId = $args['product_id'];
$stock = $args['stock'];
// $args are array holding 'productId' and 'stock' key
// here I want to update field that is inside of the repeater field
// repeater field is called 'taskItems' and every repeater item
// has fields 'product_id' and 'stock'. This is what I was trying to do:
$items = $this->data['taskItems'];

foreach($items as $item) {
if ($item['product_id'] == $productId) {
$item['stock'] = $stock;
break;
}
}
}
...
// app/Filament/Resources/TaskResource/Pages/EditTask.php
...
protected $listeners = ['update-stock' => 'updateProductStock'];

public function updateProductStock($args)
{
$productId = $args['product_id'];
$stock = $args['stock'];
// $args are array holding 'productId' and 'stock' key
// here I want to update field that is inside of the repeater field
// repeater field is called 'taskItems' and every repeater item
// has fields 'product_id' and 'stock'. This is what I was trying to do:
$items = $this->data['taskItems'];

foreach($items as $item) {
if ($item['product_id'] == $productId) {
$item['stock'] = $stock;
break;
}
}
}
...
As you can see I was trying to manipulate field values directly in $data array, but that doesn't work. When form is rendered I still see old stock value in the form.
Solution:
After some more debugging problem was the way I was updating the value. This is foreach loop that made everything working fine: ``` $items = $this->data['taskItems']; ...
Jump to solution
2 Replies
tjodalv
tjodalvOP13mo ago
Update: If I dump all the form data in updateProductStock() method I can see that I've changed the quantity, but in the form it is still the old value. After foreach loop in my updateProductStock() method I've added:
dump($this->data);
dump($this->data);
When my livewire component dispatches 'update-stock' event, my method updateProductStock() is executed and modal pops up with dumped data. I can see that I've changed stock value, but it is not reflected in the form.
Solution
tjodalv
tjodalv13mo ago
After some more debugging problem was the way I was updating the value. This is foreach loop that made everything working fine:
$items = $this->data['taskItems'];

foreach($items as $key => $item) {
if ($item['product_id'] == $productId) {
// THIS IS HOW TO UPDATE PROPERTY INSIDE DATA ARRAY
$this->data['taskItems'][$key]['stock'] = $stock;
break;
}
}
$items = $this->data['taskItems'];

foreach($items as $key => $item) {
if ($item['product_id'] == $productId) {
// THIS IS HOW TO UPDATE PROPERTY INSIDE DATA ARRAY
$this->data['taskItems'][$key]['stock'] = $stock;
break;
}
}
I think that has nothing to do with filament but rather how PHP works.
Want results from more Discord servers?
Add your server