88use Filament \Schemas \Components \Grid ;
99use Illuminate \Support \Collection ;
1010use Relaticle \CustomFields \Filament \Integration \Factories \FieldComponentFactory ;
11+ use Relaticle \CustomFields \Filament \Integration \Factories \SectionComponentFactory ;
1112use Relaticle \CustomFields \Models \CustomField ;
13+ use Relaticle \CustomFields \Models \CustomFieldSection ;
1214
1315class FormBuilder extends BaseBuilder
1416{
17+ private bool $ withoutSections = false ;
18+
1519 public function build (): Grid
1620 {
1721 return FormContainer::make ()
1822 ->forModel ($ this ->explicitModel ?? null )
23+ ->withoutSections ($ this ->withoutSections )
1924 ->only ($ this ->only )
2025 ->except ($ this ->except );
2126 }
2227
28+ public function withoutSections (bool $ withoutSections = true ): static
29+ {
30+ $ this ->withoutSections = $ withoutSections ;
31+
32+ return $ this ;
33+ }
34+
2335 private function getDependentFieldCodes (Collection $ fields ): array
2436 {
2537 $ dependentCodes = [];
@@ -40,21 +52,29 @@ private function getDependentFieldCodes(Collection $fields): array
4052 public function values (): Collection
4153 {
4254 $ fieldComponentFactory = app (FieldComponentFactory::class);
55+ $ sectionComponentFactory = app (SectionComponentFactory::class);
4356
4457 $ allFields = $ this ->getFilteredSections ()->flatMap (fn (mixed $ section ) => $ section ->fields );
4558 $ dependentFieldCodes = $ this ->getDependentFieldCodes ($ allFields );
4659
47- // Return fields directly without Section/Fieldset wrappers
48- // This ensures the flat structure: custom_fields.{field_code}
49- // Note: We skip section grouping to avoid nested paths like custom_fields.{section_code}.{field_code}
50- // which causes issues with Filament v4's child schema nesting behavior.
51- // Visual grouping can be added later using alternative methods if needed.
52- return $ allFields ->map (
53- fn (CustomField $ customField ) => $ fieldComponentFactory ->create (
54- $ customField ,
55- $ dependentFieldCodes ,
56- $ allFields
57- )
60+ $ createField = fn (CustomField $ customField ) => $ fieldComponentFactory ->create (
61+ $ customField ,
62+ $ dependentFieldCodes ,
63+ $ allFields
5864 );
65+
66+ if ($ this ->withoutSections ) {
67+ return $ allFields ->map ($ createField );
68+ }
69+
70+ return $ this ->getFilteredSections ()
71+ ->map (function (CustomFieldSection $ section ) use ($ sectionComponentFactory , $ createField ) {
72+ $ fields = $ section ->fields ->map ($ createField );
73+
74+ return $ fields ->isEmpty ()
75+ ? null
76+ : $ sectionComponentFactory ->create ($ section )->schema ($ fields ->toArray ());
77+ })
78+ ->filter ();
5979 }
6080}
0 commit comments