Customizing the Unique validation rule

How would I appropriately customize the Unique validation rule/method that Filament has according to the following? Here is my categories table:
Schema::create('categories', function (Blueprint $table) {
$table->id();
$table->foreignId('company_id')->constrained()->cascadeOnDelete();
$table->string('name')->index();
$table->string('type');
$table->string('color');
$table->boolean('enabled')->default(true);
$table->foreignId('created_by')->nullable()->constrained('users')->nullOnDelete();
$table->foreignId('updated_by')->nullable()->constrained('users')->nullOnDelete();
$table->timestamps();

$table->unique(['company_id', 'name', 'type']);
});
Schema::create('categories', function (Blueprint $table) {
$table->id();
$table->foreignId('company_id')->constrained()->cascadeOnDelete();
$table->string('name')->index();
$table->string('type');
$table->string('color');
$table->boolean('enabled')->default(true);
$table->foreignId('created_by')->nullable()->constrained('users')->nullOnDelete();
$table->foreignId('updated_by')->nullable()->constrained('users')->nullOnDelete();
$table->timestamps();

$table->unique(['company_id', 'name', 'type']);
});
This is what I have so far for the Field:
Forms\Components\TextInput::make('name')
\\ ...
->unique(modifyRuleUsing: static function (Unique $rule, Request $request) {
return $rule->where('company_id', auth()->user()->currentCompany->id)
->where('type', $request->input('type'));
}),
Forms\Components\TextInput::make('name')
\\ ...
->unique(modifyRuleUsing: static function (Unique $rule, Request $request) {
return $rule->where('company_id', auth()->user()->currentCompany->id)
->where('type', $request->input('type'));
}),
Will the above work? Or do I need to do something like this or something else?
Forms\Components\TextInput::make('name')
\\ ...
->unique(modifyRuleUsing: static function (Unique $rule, Forms\Get $get) {
return $rule->where('company_id', auth()->user()->currentCompany->id)
->where('type', $get('type'));
}),
Forms\Components\TextInput::make('name')
\\ ...
->unique(modifyRuleUsing: static function (Unique $rule, Forms\Get $get) {
return $rule->where('company_id', auth()->user()->currentCompany->id)
->where('type', $get('type'));
}),
5 Replies
ZedoX
ZedoX10mo ago
You can't use $request in livewire requests. Second approach is the way to go
cheesegrits
cheesegrits10mo ago
Yes, you can inject $request, but it's only going to work on page load (for example in the mount() method of a page), not for Livewire method calls. Livewire doesn't use HTTP requests, you're not doing a POST or a GET or whatever.
awcodes
awcodes10mo ago
And since it’s ajax based the request isn’t actually the url in the browser.
Andrew Wallo
Andrew Wallo10mo ago
Oh okay
Want results from more Discord servers?
Add your server
More Posts
Header in a Card Form builderIs it possible to add a header to a card tag by enclosing a code segment? ```php Card::make() ->scBypass automatic case alterations for labelsHi. I can't figure out how to bypass the case alterations that are happening for resource names. Fodisable lazy loading when testingIs there a way to disable lazy loading relation managers and widgets in testing environment? I wantHow to use tailwind css inside the filament custom created page?I have configured tailwind css with laravel. Inside my welcome.blade.php tailwind classes working fiHow to prevent tags in TagInput from doubling when reordering items in RepeaterI have a TagInput inside a Repeater that gets auto populated based on existing data. Every time I reDatepicker Issuewhen using the dark theme that the month dropdown in the date picker is white text on a white backgrUsing Alpine.js inside the existing app with Filament tableHi, I have a Laravel application and am using Alpine and have a Filament Table installed inside the Curator plugin acceptedFileTypes argument not being adhered to when picking from uploaded contentHi. Is anyone else using the curator plugin and also having this issue? https://github.com/awcodes/fInserting pivot data without RelationManagerI have a multiple select on an edit page, has a `belongsToMany()` relationship. I want to inject sohow can I create a custom page in filament, outside of the dashboard auth?for use of user invitation system, something with a layout like the register page, without filament