F
Filamentβ€’6mo ago
bardolf_6969

If a relationship is hidden on a resource form does filament still try to submit it?

I have a resource form that when no relationship is set displays a select box but that changes to a section displaying the details once the relationship is set. The problem is when submitting the form I'm getting an error that a null value in the relation violates not-null constraint. Below is the section of the form
Forms\Components\Select::make('user_id')
->hidden(fn (Get $get) => !empty($get('user_id')))
->columnStart(1)
->columnSpan(2)
->label('Owner')
->options(User::all()->pluck('name', 'id'))
->searchable()
->live(),
Forms\Components\Section::make('Owner Information')
->hidden(fn (Get $get) => empty($get('user_id')))
->columnSpan(2)
->relationship('owner')
->schema([
Forms\Components\Section::make('Basic Information')
->columns(2)
->schema([
Forms\Components\TextInput::make('name')
->label('Full Name')
->columnSpan(2)
->maxLength(255),
Forms\Components\TextInput::make('phone')
->tel()
->maxLength(255),
Forms\Components\TextInput::make('email')
->email()
->maxLength(255),
])
->reactive(),
Forms\Components\Section::make('Social Media')
->columns(3)
->schema([
Forms\Components\TextInput::make('line_id')
->maxLength(255)
->reactive(),
Forms\Components\TextInput::make('facebook')
->maxLength(255)
->reactive(),
Forms\Components\TextInput::make('whatsapp')
->maxLength(255)
->reactive(),
]),
])
->reactive(),
Forms\Components\Select::make('user_id')
->hidden(fn (Get $get) => !empty($get('user_id')))
->columnStart(1)
->columnSpan(2)
->label('Owner')
->options(User::all()->pluck('name', 'id'))
->searchable()
->live(),
Forms\Components\Section::make('Owner Information')
->hidden(fn (Get $get) => empty($get('user_id')))
->columnSpan(2)
->relationship('owner')
->schema([
Forms\Components\Section::make('Basic Information')
->columns(2)
->schema([
Forms\Components\TextInput::make('name')
->label('Full Name')
->columnSpan(2)
->maxLength(255),
Forms\Components\TextInput::make('phone')
->tel()
->maxLength(255),
Forms\Components\TextInput::make('email')
->email()
->maxLength(255),
])
->reactive(),
Forms\Components\Section::make('Social Media')
->columns(3)
->schema([
Forms\Components\TextInput::make('line_id')
->maxLength(255)
->reactive(),
Forms\Components\TextInput::make('facebook')
->maxLength(255)
->reactive(),
Forms\Components\TextInput::make('whatsapp')
->maxLength(255)
->reactive(),
]),
])
->reactive(),
Solution:
mutating the form data didn't work but it got me looking at the docs which is when I discovered the ability to conditionally submit the relationship πŸ™‚
Jump to solution
2 Replies
Dimitar Papazov DEV
Dimitar Papazov DEVβ€’6mo ago
You can try to dd the subitted data and check whats passing. Just add the method mutateFormDataBeforeCreate in the Create** in the resource folder. ```php // in Create(resource name).php EXAMPLE: CreatePage.php protected function mutateFormDataBeforeCreate(array $data): array { dd($data); } ``` You can check if is null and remove it from the data. Same can be done for Edit.
Solution
bardolf_6969
bardolf_6969β€’6mo ago
mutating the form data didn't work but it got me looking at the docs which is when I discovered the ability to conditionally submit the relationship πŸ™‚
Want results from more Discord servers?
Add your server