From 20d67b3a8faa4ac9a5f13b382a4067ef02d16f25 Mon Sep 17 00:00:00 2001 From: David Date: Wed, 5 Jun 2024 11:07:46 +0200 Subject: [PATCH] added included_fields option to AutoFormType --- src/Form/Manipulator/DoctrineORMManipulator.php | 7 +++++-- src/Form/Type/AutoFormType.php | 1 + 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/src/Form/Manipulator/DoctrineORMManipulator.php b/src/Form/Manipulator/DoctrineORMManipulator.php index aa37e90..11fdd73 100644 --- a/src/Form/Manipulator/DoctrineORMManipulator.php +++ b/src/Form/Manipulator/DoctrineORMManipulator.php @@ -30,7 +30,7 @@ public function getFieldsConfig(FormInterface $form): array // Filtering to remove excludedFields $objectFieldsConfig = $this->doctrineORMInfo->getFieldsConfig($class); - $validObjectFieldsConfig = $this->filteringValidObjectFields($objectFieldsConfig, $formOptions['excluded_fields']); + $validObjectFieldsConfig = $this->filteringValidObjectFields($objectFieldsConfig, $formOptions['excluded_fields'], $formOptions['included_fields']); if (empty($formOptions['fields'])) { return $validObjectFieldsConfig; @@ -86,12 +86,15 @@ private function getDataClass(FormInterface $form): string throw new \RuntimeException('Unable to get dataClass'); } - private function filteringValidObjectFields(array $objectFieldsConfig, array $formExcludedFields): array + private function filteringValidObjectFields(array $objectFieldsConfig, array $formExcludedFields, array $formIncludedFields): array { $excludedFields = array_merge($this->globalExcludedFields, $formExcludedFields); $validFields = []; foreach ($objectFieldsConfig as $fieldName => $fieldConfig) { + if (!empty($formIncludedFields) && !\in_array($fieldName, $formIncludedFields, true)) { + continue; + } if (\in_array($fieldName, $excludedFields, true)) { continue; } diff --git a/src/Form/Type/AutoFormType.php b/src/Form/Type/AutoFormType.php index c565f95..1e506a6 100644 --- a/src/Form/Type/AutoFormType.php +++ b/src/Form/Type/AutoFormType.php @@ -35,6 +35,7 @@ public function configureOptions(OptionsResolver $resolver): void $resolver->setDefaults([ 'fields' => [], 'excluded_fields' => [], + 'included_fields' => [], ]); $resolver->setNormalizer('data_class', static function (Options $options, $value): string {