Update form field from page action

Hi all, I feel like this is relatively simple Livewire stuff but I can't figure it out. I'm trying to dynamically update a field from a page action in my EditRecord class, I've tried a few different things without any luck :
class EditProduct extends EditRecord
{
// ...

public function calculatePrice()
{
$this->price_retail = 123.45;

$this->data['price_retail'] = 123.45;

$this->form->evaluate(fn (Closure $set) => $set('price_retail', 123.45));
}
}
class EditProduct extends EditRecord
{
// ...

public function calculatePrice()
{
$this->price_retail = 123.45;

$this->data['price_retail'] = 123.45;

$this->form->evaluate(fn (Closure $set) => $set('price_retail', 123.45));
}
}
Am I getting warmer?
7 Replies
Patrick Boivin
Patrick BoivinOP2y ago
I've also added ->reactive() to my field definition
Patrick Boivin
Patrick BoivinOP2y ago
Thanks for the super quick reply @Leandro Ferreira! So if I understand correctly, this would imply updating the record in the DB, then refreshing the form?
LeandroFerreira
yes
Patrick Boivin
Patrick BoivinOP2y ago
Ok great. What's different in my case is I'm trying to update the form field, without updating the record. I still want to wait for an explicit "save" action from the user.
LeandroFerreira
just use refreshFormData
Patrick Boivin
Patrick BoivinOP2y ago
I see! I'll give this a try. None of these seem to work for me :
class EditProduct extends EditRecord
{
// ...

public function calculatePrice()
{
// A
$this->data['price_retail'] = 111.11;
$this->refreshFormData(['price_retail']);

// B
$this->record->price_retail = 222.22;
$this->refreshFormData(['price_retail']);

// C
$this->record->price_retail = 333.33;
$this->record->save();
$this->refreshFormData(['price_retail']);

}
}
class EditProduct extends EditRecord
{
// ...

public function calculatePrice()
{
// A
$this->data['price_retail'] = 111.11;
$this->refreshFormData(['price_retail']);

// B
$this->record->price_retail = 222.22;
$this->refreshFormData(['price_retail']);

// C
$this->record->price_retail = 333.33;
$this->record->save();
$this->refreshFormData(['price_retail']);

}
}
Although when I refresh the page after C, my field has the updated value (obviously...). What am I missing? Ok so after a bit more debugging, this seems to work :
$this->record->price_retail = 222.22;
$this->refreshFormData(['price_retail']);
$this->record->price_retail = 222.22;
$this->refreshFormData(['price_retail']);
but my problem is that my ->formatStateUsing() logic in my field doesn't seem to be applied to the new updated value. Gaaah, turns out this is doing exactly what I want :
$this->data['price_retail'] = '111.11';
$this->data['price_retail'] = '111.11';
But in my context, the value needed to be a string to be properly handled. Thanks again @Leandro Ferreira.
Want results from more Discord servers?
Add your server