FilamentF
Filament16mo ago
nowak

How to set default date in DatePicker form field in filtersForm?

I have a custom dashboard class, where I want to add a filtersForm, with a date field that should default to todays date (now()), but the default is never being set in my form when I try to do this:
<?php

namespace App\Filament\Pages;

use App\Models\MealType;
use Filament\Forms\Components\DatePicker;
use Filament\Forms\Components\Section;
use Filament\Forms\Components\Select;
use Filament\Forms\Form;
use Filament\Pages\Dashboard as BaseDashboard;

class Dashboard extends BaseDashboard
{

  use BaseDashboard\Concerns\HasFiltersForm;
  protected static ?string $navigationIcon = 'heroicon-o-document-text';

  public function filtersForm(Form $form): Form
  {
    return $form
      ->schema([
        Section::make()
            ->schema([
              DatePicker::make('deliveryDate')
                ->default(now()),
              Select::make('mealType')
                ->options(fn() => MealType::all()
                  ->sortBy('id')
                  ->where('is_visible','true')
                  ->pluck('name', 'id')
                  ->map(function ($name) {
                    return ucfirst($name);
                  })
                )
                ->default(2)
                ->selectablePlaceholder(false)
                ->native(false)
            ])
            ->columns(2),
      ]);
  }
}


I have attached screenshots of my form field when null (on page load) and when I select a date.

Does anyone know how I can set the default date correctly?
Was this page helpful?