Preventing Duplicate 'Active' Records in Model Using Filament

Hello, I'm new to using filament, how can I prevent a new record in my SchoolDiar model from being written if another record with 'active' with value 'Ativa' already exists?
public static function form(Form $form): Form
{
return $form
->schema([
Select::make('active')
->options(['Ativa' => 'Ativa', 'Inativa' => 'Inativa'])
->native(false)
->default('Inativa')
->label('Status'),

}
public static function form(Form $form): Form
{
return $form
->schema([
Select::make('active')
->options(['Ativa' => 'Ativa', 'Inativa' => 'Inativa'])
->native(false)
->default('Inativa')
->label('Status'),

}
4 Replies
MK | Programmer
MK | ProgrammerOP8mo ago
The question is simply how to prevent a new entry if there's already another entry in the table with 'active' value 1.
MK | Programmer
MK | ProgrammerOP8mo ago
Yes, but I need a query so that this exception is related to the current school. For example: school_id = 1. I need it to search the table for everything in the SchoolDiar model and check if there already exists one with 'active' value 'Ativa'. Can we use a query in the unique() function for this?
LeandroFerreira
LeandroFerreira8mo ago
maybe modifyRuleUsing. Check the docs. You can also use a custom rule: https://filamentphp.com/docs/3.x/forms/validation#custom-rules
Want results from more Discord servers?
Add your server