The dynamic toggle button is not working and has the value null saved.

When I select an asset type in AssetResource, the AssetTypeResource's associated schema is made available for filling out, with the exception of the toggle button. I have built an AssetTypeResource where I have established the schema for a certain asset type. When I first created an asset the toggle button field, stored the null value rather than the value true/false; yet, on the edit page, the value true/false is saved.
public static function getAssetFieldsFormSchema($asset_type_id, Form $form)
{
if (empty($asset_type_id)) {
return [];
}
$fieldHandlers = [
'toggle' => function ($field, $fieldResponseKey) {
$text_input = Toggle::make($fieldResponseKey)
->label($field['field_label'])
->onIcon('heroicon-s-check')
->offIcon('heroicon-s-x');
if ($field['field_is_required'])
$text_input->required(true);
return $text_input;
},
];
$asset_type_fields = AssetType::find($asset_type_id)->fields;
$schema = [];
foreach ($asset_type_fields as $field) {
$asset_field_key = "asset_fields.{$field['field_key']}";
$fieldType = $field['field_type'];

if (isset($fieldHandlers[$fieldType])) {
$handler = $fieldHandlers[$fieldType];
$schema[] = $handler($field, $asset_field_key);
}
}
return $schema;
}
public static function getAssetFieldsFormSchema($asset_type_id, Form $form)
{
if (empty($asset_type_id)) {
return [];
}
$fieldHandlers = [
'toggle' => function ($field, $fieldResponseKey) {
$text_input = Toggle::make($fieldResponseKey)
->label($field['field_label'])
->onIcon('heroicon-s-check')
->offIcon('heroicon-s-x');
if ($field['field_is_required'])
$text_input->required(true);
return $text_input;
},
];
$asset_type_fields = AssetType::find($asset_type_id)->fields;
$schema = [];
foreach ($asset_type_fields as $field) {
$asset_field_key = "asset_fields.{$field['field_key']}";
$fieldType = $field['field_type'];

if (isset($fieldHandlers[$fieldType])) {
$handler = $fieldHandlers[$fieldType];
$schema[] = $handler($field, $asset_field_key);
}
}
return $schema;
}
10 Replies
Patrick Boivin
Patrick Boivin17mo ago
Sorry, what is the issue? Is your Toggle field saved as null instead of false?
Skjena
SkjenaOP17mo ago
yes
Patrick Boivin
Patrick Boivin17mo ago
What if you activate the Toggle, does it save as true instead of null?
Skjena
SkjenaOP17mo ago
No
Patrick Boivin
Patrick Boivin17mo ago
It's going to be hard to help you without more code and context... What have you tried?
LeandroFerreira
LeandroFerreira17mo ago
->dehydrateStateUsing(fn ($state): ?bool => $state ?: null)
->dehydrateStateUsing(fn ($state): ?bool => $state ?: null)
?
Skjena
SkjenaOP17mo ago
I tried that as well, but it didn't work. When I create something for the first time, this code continues to save the data as null. I want to store the true/false value.
Patrick Boivin
Patrick Boivin17mo ago
Have you tried a boolean cast, on the model?
protected $casts = [
'my_field' => 'boolean',
];
protected $casts = [
'my_field' => 'boolean',
];
LeandroFerreira
LeandroFerreira17mo ago
Sorry, forget my code. I thought that you were trying to set false equal to null This is the default behavior
Skjena
SkjenaOP17mo ago
This won't work any more because my_field is a column of the json type, and I want to store it in key-value format, as in {"my_key": false}.
Want results from more Discord servers?
Add your server