Hidden field works only while create and not edit

Hi, i have 2 dependant fields

Section::make('Customer type and company')
->schema([
Radio::make('customer_type')
->label('Customer type')
->options(CustomerType::class)
->default(CustomerType::PRIVATE)
->inline()
->live()
->columnSpan(1),
Select::make('company_id')
->visible(fn ( Get $get) => $get('customer_type') === CustomerType::BUSINESS)
->relationship(name: 'companies', titleAttribute: 'name')
->preload()
->multiple()
->native(false)
->searchable()
->columnSpan(2),
])->columns(2),

Section::make('Customer type and company')
->schema([
Radio::make('customer_type')
->label('Customer type')
->options(CustomerType::class)
->default(CustomerType::PRIVATE)
->inline()
->live()
->columnSpan(1),
Select::make('company_id')
->visible(fn ( Get $get) => $get('customer_type') === CustomerType::BUSINESS)
->relationship(name: 'companies', titleAttribute: 'name')
->preload()
->multiple()
->native(false)
->searchable()
->columnSpan(2),
])->columns(2),
when i select the BUSINESS option, a select is visible but it works only when i create a new customer in edit mode, doesn't works.
Solution:
otherwise you would have to check against the actual value $get('customer_type') === CustomerType::BUSINESS->value
Jump to solution
8 Replies
LeandroFerreira
LeandroFerreira7mo ago
Do you mean, when you change the radio value in the edit page? It appears that $get('customer_type') === CustomerType::BUSINESS might not be matching as expected (customer_type value is from database in the edit operation)
awcodes
awcodes7mo ago
You are casting the field in you model to the enum?
Soundmit
SoundmitOP7mo ago
hi, this is my enum
<?php
namespace App\Enums;

use Filament\Support\Contracts\HasIcon;
use Filament\Support\Contracts\HasLabel;
use Filament\Support\Contracts\HasColor;

enum CustomerType: string implements HasLabel, HasColor, HasIcon
{
case BUSINESS = 'business';
case PRIVATE = 'private';

public function getLabel(): ?string
{
return match ($this) {
self::BUSINESS => __('customer.business'),
self::PRIVATE => __('customer.private'),
};
}


}
<?php
namespace App\Enums;

use Filament\Support\Contracts\HasIcon;
use Filament\Support\Contracts\HasLabel;
use Filament\Support\Contracts\HasColor;

enum CustomerType: string implements HasLabel, HasColor, HasIcon
{
case BUSINESS = 'business';
case PRIVATE = 'private';

public function getLabel(): ?string
{
return match ($this) {
self::BUSINESS => __('customer.business'),
self::PRIVATE => __('customer.private'),
};
}


}
no casts i nthe customer model
awcodes
awcodes7mo ago
you need to cast it on the model if you are expecting it to be the enum on the model instance
Solution
awcodes
awcodes7mo ago
otherwise you would have to check against the actual value $get('customer_type') === CustomerType::BUSINESS->value
Soundmit
SoundmitOP7mo ago
thanks, i will try
Tieme
Tieme7mo ago
@Soundmit i hade the same issue. I have cast on the model and use a "helper function" so it will work on create and edit form sidenote : All my enums are numeric so possible the code can be optimzed to meet you needs. Somethin like instance of enum
class Helpers{

/**
* Retrieves the value from an enumeration.
*
* @param mixed $enum The enumeration value.
* @return mixed The value from the enumeration.
*/
public static function getValueFromEnum(mixed $enum)
{
if (is_numeric($enum) or is_null($enum)) {
return $enum;
}

return $enum->value;
}

}

->visible(fn ( Get $get) => Helpers::getValueFromEnum($get('customer_type')) === CustomerType::BUSINESS->value)
class Helpers{

/**
* Retrieves the value from an enumeration.
*
* @param mixed $enum The enumeration value.
* @return mixed The value from the enumeration.
*/
public static function getValueFromEnum(mixed $enum)
{
if (is_numeric($enum) or is_null($enum)) {
return $enum;
}

return $enum->value;
}

}

->visible(fn ( Get $get) => Helpers::getValueFromEnum($get('customer_type')) === CustomerType::BUSINESS->value)
Soundmit
SoundmitOP7mo ago
this solve ->visible(fn (Get $get) => $get('customer_type') === CustomerType::BUSINESS->value) thanks
Want results from more Discord servers?
Add your server