From f7eb772f563d86d98915443e355c7d2364b7db29 Mon Sep 17 00:00:00 2001 From: Dan Burzynski Date: Wed, 14 Feb 2018 13:52:57 +0000 Subject: [PATCH] If a trigger is specified at a form level, if there is a default from the configuration, we ignore it --- src/Form/Extension/ParsleyTypeExtension.php | 19 ++++++---- tests/Fixtures/Entity/TriggerEntity.php | 27 ++++++++++++++ tests/Fixtures/Form/Type/TestTriggerType.php | 35 +++++++++++++++++++ .../Extension/ParsleyTypeExtensionTest.php | 24 +++++++++++++ 4 files changed, 99 insertions(+), 6 deletions(-) create mode 100644 tests/Fixtures/Entity/TriggerEntity.php create mode 100644 tests/Fixtures/Form/Type/TestTriggerType.php diff --git a/src/Form/Extension/ParsleyTypeExtension.php b/src/Form/Extension/ParsleyTypeExtension.php index 1a5db2c..3544ff2 100644 --- a/src/Form/Extension/ParsleyTypeExtension.php +++ b/src/Form/Extension/ParsleyTypeExtension.php @@ -214,16 +214,23 @@ private function getConstraintsFromForm(FormInterface $form): array * @param FormView $view * @param DirectiveInterface[] $directives */ - private function addParsleyToView(FormView $view, array $directives) + private function addParsleyToView(FormView $view, array $directives): void { $attr = []; - if (count($directives) > 0 && $this->getConfig()['field']['trigger'] !== null) { - $directives[] = new Trigger($this->getConfig()['field']['trigger']); - } - foreach ($directives as $constraint) { - foreach ($constraint->getViewAttr() as $key => $value) { + $hasTrigger = false; + + foreach ($directives as $directive) { + foreach ($directive->getViewAttr() as $key => $value) { $attr[$key] = $value; } + if ($directive instanceof Trigger) { + $hasTrigger = true; + } + } + + if (!$hasTrigger && count($directives) > 0 && $this->getConfig()['field']['trigger'] !== null) { + $trigger = new Trigger($this->getConfig()['field']['trigger']); + $attr = array_merge($attr, $trigger->getViewAttr()); } if (count($attr) > 0) { diff --git a/tests/Fixtures/Entity/TriggerEntity.php b/tests/Fixtures/Entity/TriggerEntity.php new file mode 100644 index 0000000..c0250c2 --- /dev/null +++ b/tests/Fixtures/Entity/TriggerEntity.php @@ -0,0 +1,27 @@ +check; + } + + /** + * @param bool $check + * @return TriggerEntity + */ + public function setCheck(bool $check) + { + $this->check = $check; + + return $this; + } +} diff --git a/tests/Fixtures/Form/Type/TestTriggerType.php b/tests/Fixtures/Form/Type/TestTriggerType.php new file mode 100644 index 0000000..f91f9ca --- /dev/null +++ b/tests/Fixtures/Form/Type/TestTriggerType.php @@ -0,0 +1,35 @@ +add( + 'check', + CheckboxType::class, + [ + 'parsleys' => [ + new Required(), + new Trigger('click') + ], + ] + ) + ; + } + + public function configureOptions(OptionsResolver $resolver) + { + $resolver->setDefaults(['data_class' => TriggerEntity::class]); + } +} diff --git a/tests/Form/Extension/ParsleyTypeExtensionTest.php b/tests/Form/Extension/ParsleyTypeExtensionTest.php index d9a5cca..719fcad 100644 --- a/tests/Form/Extension/ParsleyTypeExtensionTest.php +++ b/tests/Form/Extension/ParsleyTypeExtensionTest.php @@ -3,6 +3,7 @@ namespace C0ntax\ParsleyBundle\Tests\Form\Extension; use C0ntax\ParsleyBundle\Tests\Fixtures\Form\Type\TestRemovalType; +use C0ntax\ParsleyBundle\Tests\Fixtures\Form\Type\TestTriggerType; use C0ntax\ParsleyBundle\Tests\Fixtures\Form\Type\TestType; use C0ntax\ParsleyBundle\Tests\Form\AbstractTypeTestCase; use Symfony\Component\Form\FormInterface; @@ -70,6 +71,20 @@ public function testRemoval() ); } + public function testTriggerOverride() + { + $form = $this->createTriggerForm(); + $view = $form->createView(); + self::assertEquals( + [ + 'data-parsley-required' => 'true', + 'data-parsley-trigger' => 'click', + ], + $view->children['check']->vars['attr'] + ); + + } + protected function getParsleyTypeConfig() { return [ @@ -98,4 +113,13 @@ private function createRemovalForm(): FormInterface return $this->factory->create(TestRemovalType::class); } + /** + * @return FormInterface + * @throws \Symfony\Component\OptionsResolver\Exception\InvalidOptionsException + */ + private function createTriggerForm(): FormInterface + { + return $this->factory->create(TestTriggerType::class); + } + }