diff --git a/src/Filament/Integration/Builders/FormBuilder.php b/src/Filament/Integration/Builders/FormBuilder.php index 4382575d..4e1c4a7e 100644 --- a/src/Filament/Integration/Builders/FormBuilder.php +++ b/src/Filament/Integration/Builders/FormBuilder.php @@ -13,6 +13,7 @@ use Relaticle\CustomFields\Filament\Integration\Factories\SectionComponentFactory; use Relaticle\CustomFields\Models\CustomField; use Relaticle\CustomFields\Models\CustomFieldSection; +use Relaticle\CustomFields\Services\Visibility\CoreVisibilityLogicService; class FormBuilder extends BaseBuilder { @@ -42,16 +43,14 @@ public function withoutSections(bool $withoutSections = true): static private function getDependentFieldCodes(Collection $fields): array { + $service = app(CoreVisibilityLogicService::class); $dependentCodes = []; foreach ($fields as $field) { - if ($field->visibility_conditions && is_array($field->visibility_conditions)) { - foreach ($field->visibility_conditions as $condition) { - if (isset($condition['field'])) { - $dependentCodes[] = $condition['field']; - } - } - } + $dependentCodes = array_merge( + $dependentCodes, + $service->getDependentFields($field), + ); $validationRules = $field->validation_rules; if ($validationRules) { diff --git a/tests/Feature/Integration/FormBuilderStrictModeTest.php b/tests/Feature/Integration/FormBuilderStrictModeTest.php new file mode 100644 index 00000000..28739076 --- /dev/null +++ b/tests/Feature/Integration/FormBuilderStrictModeTest.php @@ -0,0 +1,128 @@ +user = User::factory()->create(); + $this->actingAs($this->user); + + config()->set('custom-fields.features', FeatureConfigurator::configure() + ->enable( + CustomFieldsFeature::FIELD_CONDITIONAL_VISIBILITY, + CustomFieldsFeature::UI_TABLE_COLUMNS, + CustomFieldsFeature::UI_TOGGLEABLE_COLUMNS, + CustomFieldsFeature::UI_TABLE_FILTERS, + CustomFieldsFeature::SYSTEM_MANAGEMENT_INTERFACE, + CustomFieldsFeature::SYSTEM_SECTIONS, + ) + ); + + $this->section = CustomFieldSection::factory() + ->forEntityType(Post::class) + ->create(); +}); + +afterEach(function (): void { + // Disable strict mode after each test so other tests are not affected. + Model::preventAccessingMissingAttributes(false); +}); + +describe('FormBuilder strict-mode regression (issue #149)', function (): void { + it('does not throw MissingAttributeException when Model::preventAccessingMissingAttributes() is enabled and a field has visibility conditions', function (): void { + // Arrange — two custom fields; the second is conditionally visible based on the first. + $controlField = CustomField::factory()->create([ + 'custom_field_section_id' => $this->section->id, + 'name' => 'Control field', + 'code' => 'control_field', + 'type' => 'select', + 'entity_type' => Post::class, + ]); + + $dependentField = CustomField::factory()->create([ + 'custom_field_section_id' => $this->section->id, + 'name' => 'Dependent field', + 'code' => 'dependent_field', + 'type' => 'text', + 'entity_type' => Post::class, + 'settings' => [ + 'visibility' => [ + 'mode' => VisibilityMode::SHOW_WHEN, + 'logic' => VisibilityLogic::ALL, + 'conditions' => [[ + 'field_code' => 'control_field', + 'operator' => VisibilityOperator::IS_NOT_EMPTY, + 'value' => null, + 'source' => ConditionSource::CustomField, + ]], + ], + ], + ]); + + // Enable Laravel strict mode — this is the exact flag that triggers + // MissingAttributeException on unknown attribute access. + Model::preventAccessingMissingAttributes(); + + // Act — building the form must not throw. + $builder = app(FormBuilder::class)->forModel(new Post); + + expect(fn () => $builder->values())->not->toThrow(\Illuminate\Database\Eloquent\MissingAttributeException::class); + }); + + it('correctly resolves dependent field codes via CoreVisibilityLogicService in strict mode', function (): void { + // Arrange + $controlField = CustomField::factory()->create([ + 'custom_field_section_id' => $this->section->id, + 'name' => 'Status', + 'code' => 'status', + 'type' => 'select', + 'entity_type' => Post::class, + ]); + + $dependentField = CustomField::factory()->create([ + 'custom_field_section_id' => $this->section->id, + 'name' => 'Priority', + 'code' => 'priority', + 'type' => 'text', + 'entity_type' => Post::class, + 'settings' => [ + 'visibility' => [ + 'mode' => VisibilityMode::SHOW_WHEN, + 'logic' => VisibilityLogic::ALL, + 'conditions' => [[ + 'field_code' => 'status', + 'operator' => VisibilityOperator::IS_NOT_EMPTY, + 'value' => null, + 'source' => ConditionSource::CustomField, + ]], + ], + ], + ]); + + Model::preventAccessingMissingAttributes(); + + // Act — render via Livewire to exercise the full FormContainer → FormBuilder path. + $test = livewire(CreatePost::class); + + // Assert — the form renders without exception. + $test->assertSuccessful(); + }); +});