Including form fields (defined in database) in Resources Forms
My goal is to have an EventsResource form that is extendable with additional form fields that are displayed/fillable (but not hardcoded in the resource form) based on an event form type. I currently have a working FormsResource that allows me to create the additional form fields (stored in a form_fields DB table) and specify the database table name that will house the extended form data (once the form is locked and the table generated). So, standard event information is stored in the events table, extended event/form data is stored in a separate/custom form table. Event table has a form_id column, form table holds the custom table name (serving as a pivot) and the custom table will have the event_id for joining the records).
Schema::create('events', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->unsignedBigInteger('form_id')->nullable();
$table->boolean('status')->default(0);
$table->timestamps();
Schema::create('forms', function (Blueprint $table) {
$table->id();
$table->text('name');
$table->string('tablename');
$table->unsignedTinyInteger('columns')->default(4);
$table->boolean('locked')->default(0);
$table->boolean('active')->default(0);
$table->timestamps();
});
Schema::create('form_fields', function (Blueprint $table) {
$table->id();
$table->foreignId('form_id')->constrained()->cascadeOnDelete();
$table->integer('order');
$table->string('type');
$table->string('label');
$table->string('hint')->nullable();
$table->boolean('required');
$table->json('rules')->nullable();
$table->json('options')->nullable();
$table->timestamps();
});
I've been using https://github.com/TappNetwork/Filament-Form-Builder as my helpful guide (started with https://github.com/danharrin/form-builder and was overwhelmed analyzing https://github.com/lara-zeus/bolt). All those display the generated form as a separate Livewire component, their purpose/intention is different than mine. My challenge is I want to include the form fields with the resource form, ideally in some fieldset/grid; ideally all being saved by a single Save button click.
I see where I can use a function in the resource form to include additional fields:
class EventsResource extends Resource
{
protected static ?string $model = Event::class;
protected static ?string $navigationIcon = 'heroicon-o-rectangle-stack';
public static function form(Form $form): Form
{
return $form
->schema([
TextInput::make('name')
->required()
->label('Event Name'),
// ...
self::_makeAdditionalFormFieldsSection(),
]);
}
private static function _makeAdditionalFormFieldsSection () : Section
{
return Section::make('Submitter Details')->columns(2)
->schema([
Forms\Components\TextInput::make('company_name')
->label('Name')
->maxLength(100)
->required(),
])->default('0'),
]);
}
This seems like it might hold my solution, but I've been working for hours on trying to pull in the form fields from the database and generate them without any success. I'm starting to worry I'm chasing a solution that isn't (currently) feasible with Filament. I had a similar setup working for years in a CMS Framework (Joomla) that I coded, so I'm coming at the problem with what was working in that design. Maybe I'm approaching this wrong...
Any feedback is greatly appreciated!
Please or to participate in this conversation.