filamentphp / filament

A collection of beautiful full-stack components for Laravel. The perfect starting point for your next app. Using Livewire, Alpine.js and Tailwind CSS.
https://filamentphp.com
MIT License
18.09k stars 2.83k forks source link

The createOptionUsing method does not override the standard behavior #10401

Closed iAmKevinMcKee closed 9 months ago

iAmKevinMcKee commented 9 months ago

Package

filament/filament

Package Version

v3.1.24

Laravel Version

v10.38.0

Livewire Version

v3.3.2

PHP Version

PHP 8.2

Problem description

When you are trying to create a new option from a Select with ->createOptionForm and you want to customize the behavior using ->createOptionUsing, this does not work. Filament still tries to create and save the related model based on the form inputs.

For example:

Forms\Components\Select::make('state_id')
    ->createOptionForm([
        Forms\Components\TextInput::make('name'),
        Forms\Components\TextInput::make('not_present'),
    ])
    ->createOptionUsing(function ($data) {

        // I want to do something with $data['not_present'] before I create the State model

        // However, this code will never run because before we get here, Filament will try to create the option with both input fields and we will get an exception: Column not found: 1054 Unknown column 'not_present' in 'field list'

        return State::create(['name' => $data['name'])->id;
    })
    ->relationship('state', 'name')

In this case, my State model only has a name. However, if I want to collect some other data like in the not_present text input, and then do something else with it before creating the state, I can't do it.

Expected behavior

I expect ->createOptionUsing(...) will prevent the default behavior and give me full control over what I do with the user input.

Steps to reproduce

image

Reproduction repository

https://github.com/iAmKevinMcKee/filtest

Relevant log output

https://flareapp.io/share/VP6qx6xm
petrisorcraciun commented 9 months ago

The problem is related to the order of the methods. In relationship you have a method createOptionUsing, try: Forms\Components\Select::make('state_id')->relationship(...)-> createOptionUsing(...); I haven't tested, I might be wrong, but from what I remember, it should work