Fields depending on reactive PHP Enum type #2199
reinier-dubbelglas
started this conversation in
General
Replies: 1 comment
-
This is just to do with how Livewire & Eloquent casts enum properties I'm afraid, so you'll have to handle both string and Enum object cases: Components\Select::make('calculator_type')
->options(CalculatorType::cases())
->required()
->reactive()
->default(CalculatorType::Unit)
->disablePlaceholderSelection(),
Components\TextInput::make('price')
->required()
->prefix('€')
->hidden(function (Closure $get) => {
$calculatorType = $get('calculator_type');
if (is_string($calculatorType)) {
$calculatorType = CalculatorType::from($calculatorType);
}
return ! $calculatorType->hasPrices();
}),
Components\TextInput::make('percentage')
->suffix('%')
->default(0)
->required()
->hidden(function (Closure $get) => {
$calculatorType = $get('calculator_type');
if (is_string($calculatorType)) {
$calculatorType = CalculatorType::from($calculatorType);
}
return $calculatorType === CalculatorType::Percentage;
}), |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Package
filament/filament
Package Version
v2.11.1
Laravel Version
v9.9.0
Livewire Version
v2.10.5
PHP Version
PHP 8.1.2
Bug description
In my project I deal with products which are calculating their price depending on a PHP Enum value:
CalculatorType
. This value is saved with theProduct
model in the database. In myProductResource
class (generated with Filament), it works fine with Enum casting and I can succesfully save the value to the database.Based on the value of the CalculatorType I have to conditionally show/hide fields that I need for the calculation. For example, if I need to calculate percentages, I would show a field for percentages and hide the field for the price in some currency.
In
ProductResource
that looks like this:However, this gives the following error:
App\Enums\CalculatorType::from(): Argument #1 ($value) must be of type string, App\Enums\CalculatorType given
Strangely enough, this only happens when creating a NEW product. When editing a product, this error does not occur. My guess is because of the stringified CalculatorType stored in the database takes over.
Steps to reproduce
ProductResource.php
CalculatorType.php
Product.php
Relevant log output
No response
Beta Was this translation helpful? Give feedback.
All reactions