To access the current model in the form method of a Filament form, you can use the getCurrentRecord method of the form instance. This method returns the current model instance being edited in the form.
Here's an example of how you can use it to set the color of a Flatpickr field based on whether the expiration date has passed:
use App\Models\Item;
use Filament\Forms\Form;
use Filament\Forms\Fields\Flatpickr;
public static function form(Form $form): Form
{
$itemModel = $form->getCurrentRecord();
return $form
->schema([
Flatpickr::make('read_at')
->hintColor(
$itemModel->expire->isPast() ? 'danger' : 'primary',
'primary'
),
// ...
]);
}
In this example, we're using the getCurrentRecord method to get the current Item model instance being edited in the form. We then use this instance to check whether the expiration date has passed, and set the color of the Flatpickr field accordingly.
Note that you'll need to import the Item model at the top of your file if you haven't already done so.