© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
FilamentF
Filament•16mo ago•
6 replies
Trauma Zombie

How to validate Select relation field?

Hi guys, I have 3 simple models: Organization, Store, Contact. Each Contact can be assign to Store only of his own Organization. I want to use Select field with multiple option, but I need to validate that the stores saved in belongs to organization of that contact.

Forms\Components\Select::make('organization_id')
    ->label(__('Organization'))
    ->relationship('organization', 'name')
    ->preload()
    ->searchable()
    ->required()
    ->exists('organizations', 'id');

Forms\Components\Select::make('stores')
    ->label(__('Stores'))
    ->relationship(
        'stores',
        'name',
        function (Builder $query, Forms\Get $get): void {
            $organization = Organization::find($get('organization_id'));

            if ($organization) {
                $query->where('organization_id', $organization->id);
            }
        }
    )
    ->preload()
    ->multiple(),
Forms\Components\Select::make('organization_id')
    ->label(__('Organization'))
    ->relationship('organization', 'name')
    ->preload()
    ->searchable()
    ->required()
    ->exists('organizations', 'id');

Forms\Components\Select::make('stores')
    ->label(__('Stores'))
    ->relationship(
        'stores',
        'name',
        function (Builder $query, Forms\Get $get): void {
            $organization = Organization::find($get('organization_id'));

            if ($organization) {
                $query->where('organization_id', $organization->id);
            }
        }
    )
    ->preload()
    ->multiple(),

This works just fine, but user can select Organization first, then select some Stores, then change organization and click save. It will save organization with stores that dont belongs to that organization.
Filament banner
FilamentJoin
A powerful open source UI framework for Laravel • Build and ship admin panels & apps fast with Livewire
20,307Members
Resources

Similar Threads

Was this page helpful?
Recent Announcements

Similar Threads

Select field hasMany relation
FilamentFFilament / ❓┊help
3y ago
How to validate a single field?
FilamentFFilament / ❓┊help
3y ago
Select field with modifyQueryUsing doesn't validate client input
FilamentFFilament / ❓┊help
3y ago
validate select repeater option
FilamentFFilament / ❓┊help
3y ago