Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

raobilal4822's avatar

Radio button in filament by default selected true not working

i am working in filament admin panel forms i have write this code but the radio button is not selected true by default

 \Filament\Forms\Components\Radio::make('value')
                                            ->label('Value')
                                            ->live()
                                            ->options([
                                                'true' => 'True',
                                                'false' => 'False',
                                            ])
                                            ->default(function ($get) {
                                                return 'true';})
                                        ->visible(function ($get) {
                                            $moduleName = $get('../../module');
                                            $fieldName = $get('field');
                                            if (!$moduleName || !$fieldName) return false;
                                            $module = ModuleEnum::fromName($moduleName);
                                            return $module->getFieldType($fieldName) === 'boolean';
                                        })
                                        ->reactive(),
0 likes
1 reply
LaryAI's avatar
Level 58

Certainly! The issue is likely due to how the default value is being set for the radio button in Filament. The ->default() method should be passed a value directly (or a closure that returns a value), but if the form model already has a value for 'value', the default won't override it.

A few things to check and adjust:

  1. Ensure the default value matches the option key:
    Your options are 'true' and 'false' (strings), so your default should also be 'true' (string), not true (boolean).

  2. Set default directly if no dynamic logic is needed:
    If you always want 'true' selected by default, use:

    ->default('true')
    
  3. If using a closure, make sure it returns 'true':
    Your closure is fine as long as it returns 'true'.

  4. Check for model values:
    If the form is bound to a model and the model has a value for 'value', the default will not be used. Make sure the model's attribute is null or not set.

  5. Full working example:

    \Filament\Forms\Components\Radio::make('value')
        ->label('Value')
        ->live()
        ->options([
            'true' => 'True',
            'false' => 'False',
        ])
        ->default('true')
        ->visible(function ($get) {
            $moduleName = $get('../../module');
            $fieldName = $get('field');
            if (!$moduleName || !$fieldName) return false;
            $module = ModuleEnum::fromName($moduleName);
            return $module->getFieldType($fieldName) === 'boolean';
        })
        ->reactive(),
    

Summary of steps:

  • Use 'true' (string) as the default value.
  • Ensure the model does not already have a value for 'value'.
  • If you need dynamic logic, ensure your closure returns 'true'.

If you still have issues, try clearing the browser cache or check for any JavaScript errors that might interfere with Filament's reactivity.

Please or to participate in this conversation.