How to format dependable, readonly(calculated) fields?

I have a form with 3 TextInput fields: * amount (user enters value here) * price (comes from db) * sum (calculated field/value)
Forms\Components\TextInput::make('amount')
->numeric()
->reactive()
->afterStateUpdated(function (callable $set, callable $get, $state) {
$this->calculate($set, $get);
})
->required(),
Forms\Components\TextInput::make('price')
->extraInputAttributes(['readonly' => true]),
Forms\Components\TextInput::make('sum')
->extraInputAttributes(['readonly' => true]),
Forms\Components\TextInput::make('amount')
->numeric()
->reactive()
->afterStateUpdated(function (callable $set, callable $get, $state) {
$this->calculate($set, $get);
})
->required(),
Forms\Components\TextInput::make('price')
->extraInputAttributes(['readonly' => true]),
Forms\Components\TextInput::make('sum')
->extraInputAttributes(['readonly' => true]),
I can use Mask for amount field but how can i format read-only fields price and sum?
4 Replies
toeknee
toeknee2y ago
$this->calculate i am unsure what you are doing in this with the getters and setters. But you would tend to set it. You can add ->disabled() I do believe to make them readonly.
Brandon
Brandon2y ago
Hi, do you mind to share the tricks how you to populate the sum
toeknee
toeknee2y ago
Looks like it's done with $this->Calculate and passing the settings and getters so will be setting the sum in the calculate formation. Only updates on changing the amount.
Gaspar
GasparOP2y ago
public function calculate(callable $set, callable $get) { $amount = floatval($get('amount')); $price = floatval($get('price')); $sum = $price * $amount; $set('sum', $this->formatNumber($sum)); }

Did you find this page helpful?