FilamentF
Filament3y ago
13 replies
Skjena

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;
    }
Was this page helpful?