Skip to content

Commit

Permalink
Add form listener to fix file type value (#115)
Browse files Browse the repository at this point in the history
* Add form listener to fix file type value

* Fix github workflow
  • Loading branch information
JakobTolkemit authored Aug 19, 2022
1 parent f5222ad commit 72e9b7d
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 0 deletions.
1 change: 1 addition & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ jobs:
name: Restrict Symfony version
if: matrix.symfony != ''
run: |
composer global config --no-plugins allow-plugins.symfony/flex false
composer global require --no-progress --no-scripts --no-plugins "symfony/flex:^1.10"
composer config extra.symfony.require "${{ matrix.symfony }}"
Expand Down
3 changes: 3 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -61,5 +61,8 @@
"fix": [
"vendor/bin/ecs check --ansi --no-progress-bar src/ tests/PHPUnit --config etc/coding-standard.php --fix"
]
},
"config": {
"allow-plugins": false
}
}
15 changes: 15 additions & 0 deletions src/Form/Product/ShopCustomerOptionType.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ public function buildForm(FormBuilderInterface $builder, array $options): void

// Add a form field for every customer option
$customerOptions = $product->getCustomerOptions();
$customerOptionTypesByCode = [];

foreach ($customerOptions as $customerOption) {
$customerOptionType = $customerOption->getType();
Expand All @@ -84,6 +85,8 @@ public function buildForm(FormBuilderInterface $builder, array $options): void
$fieldConfig['mapped'] = $options['mapped'];

$builder->add($fieldName, $class, $fieldConfig);

$customerOptionTypesByCode[$fieldName] = $customerOptionType;
}

$builder->addEventListener(
Expand All @@ -99,6 +102,18 @@ static function (FormEvent $event): void {
}
}
);

$builder->addEventListener(FormEvents::PRE_SUBMIT, static function(FormEvent $event) use ($customerOptionTypesByCode) : void {
$data = $event->getData();

foreach ($data as $customerOptionCode => $customerOptionValue) {
if (CustomerOptionTypeEnum::FILE === $customerOptionTypesByCode[$customerOptionCode]) {
$data[$customerOptionCode] = $customerOptionValue['data'];
}
}

$event->setData($data);
});
}

public function configureOptions(OptionsResolver $resolver): void
Expand Down

0 comments on commit 72e9b7d

Please sign in to comment.