Filament select custom validation

  Forms\Components\Select::make('time_id') 
                    ->label('Time')  
                    ->relationship(name: 'time', titleAttribute: 'name') 
                    ->disableOptionWhen(function (string $value, Get $get, string $operation,string $state): bool {
                        $findSlotNotAvailable = Slot::where('date', $get('date'))->get()->pluck('time_id')->toArray();
                        if($operation == 'edit'){
                            return in_array($value, $findSlotNotAvailable) && $value != $state;
                        }
                        return in_array($value, $findSlotNotAvailable);
                    })
                    ->rules([
                        fn (Get $get, string $operation,string $state): Closure => function (string $attribute, $value, Closure $fail) use($get, $operation,$state) {
                            $findSlotNotAvailable = Slot::where('date', $get('date'))->get()->pluck('time_id')->toArray();
                            if($operation == 'edit'){
                                if(in_array($value, $findSlotNotAvailable) && $value != $state){
                                    $fail('The :attribute is not available.');
                                };
                            }else{
                                if(in_array($value, $findSlotNotAvailable)){
                                    $fail('The :attribute is not available.');
                                };
                            }

                        
                        },
                    ])
                    ->required(),

Posted

in

by

Tags:

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *