From 77536039bc1dee417c840540fa370205b4035d4f Mon Sep 17 00:00:00 2001 From: Dan Burzynski Date: Wed, 14 Feb 2018 13:40:06 +0000 Subject: [PATCH 1/3] Introduced a specific type for field triggering --- src/Form/Extension/ParsleyTypeExtension.php | 4 +- src/Parsleys/Directive/Field/Trigger.php | 68 +++++++++++++++++++ .../Parsleys/Directive/Field/TriggerTest.php | 34 ++++++++++ 3 files changed, 104 insertions(+), 2 deletions(-) create mode 100644 src/Parsleys/Directive/Field/Trigger.php create mode 100644 tests/Parsleys/Directive/Field/TriggerTest.php diff --git a/src/Form/Extension/ParsleyTypeExtension.php b/src/Form/Extension/ParsleyTypeExtension.php index 0a528fd..1a5db2c 100644 --- a/src/Form/Extension/ParsleyTypeExtension.php +++ b/src/Form/Extension/ParsleyTypeExtension.php @@ -8,7 +8,7 @@ use C0ntax\ParsleyBundle\Contracts\ParsleyInterface; use C0ntax\ParsleyBundle\Contracts\RemoveInterface; use C0ntax\ParsleyBundle\Factory\ConstraintFactory; -use C0ntax\ParsleyBundle\Parsleys\Directive\Field\Generic; +use C0ntax\ParsleyBundle\Parsleys\Directive\Field\Trigger; use C0ntax\ParsleyBundle\Parsleys\RemoveParsleyConstraint; use C0ntax\ParsleyBundle\Parsleys\RemoveSymfonyConstraint; use Symfony\Component\Form\AbstractTypeExtension; @@ -218,7 +218,7 @@ private function addParsleyToView(FormView $view, array $directives) { $attr = []; if (count($directives) > 0 && $this->getConfig()['field']['trigger'] !== null) { - $directives[] = new Generic('trigger', $this->getConfig()['field']['trigger']); + $directives[] = new Trigger($this->getConfig()['field']['trigger']); } foreach ($directives as $constraint) { foreach ($constraint->getViewAttr() as $key => $value) { diff --git a/src/Parsleys/Directive/Field/Trigger.php b/src/Parsleys/Directive/Field/Trigger.php new file mode 100644 index 0000000..e758768 --- /dev/null +++ b/src/Parsleys/Directive/Field/Trigger.php @@ -0,0 +1,68 @@ +setValue($value); + } + + /** + * @return array + */ + public function getViewAttr(): array + { + return [ + implode('-', [AbstractConstraint::DATA_ATTRIBUTE_PREFIX, $this->getKey()]) => $this->getValue(), + ]; + } + + /** + * @return string + */ + private function getKey(): string + { + return $this->key; + } + + /** + * @return string + */ + private function getValue(): string + { + return $this->value; + } + + /** + * @param string $value + * @return Trigger + */ + private function setValue(string $value): Trigger + { + $this->value = $value; + + return $this; + } +} diff --git a/tests/Parsleys/Directive/Field/TriggerTest.php b/tests/Parsleys/Directive/Field/TriggerTest.php new file mode 100644 index 0000000..3b5d0bd --- /dev/null +++ b/tests/Parsleys/Directive/Field/TriggerTest.php @@ -0,0 +1,34 @@ +getViewAttr()); + } + + /** + * @return array + */ + public function createGetViewAttrTestData(): array + { + return [ + [ + 'value' => 'click', + 'expected' => ['data-parsley-trigger' => 'click'], + ], + [ + 'value' => 'change focus', + 'expected' => ['data-parsley-trigger' => 'change focus'], + ], + ]; + } +} From f7eb772f563d86d98915443e355c7d2364b7db29 Mon Sep 17 00:00:00 2001 From: Dan Burzynski Date: Wed, 14 Feb 2018 13:52:57 +0000 Subject: [PATCH 2/3] 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); + } + } From 1785cb7713ffcc23a8c17d5020d4df1c9c111a44 Mon Sep 17 00:00:00 2001 From: Dan Burzynski Date: Wed, 14 Feb 2018 13:53:30 +0000 Subject: [PATCH 3/3] Feature add --- composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/composer.json b/composer.json index 2806f4c..d0b1a4d 100644 --- a/composer.json +++ b/composer.json @@ -1,6 +1,6 @@ { "name": "c0ntax/parsley-bundle", - "version": "0.5.0", + "version": "0.6.0", "type": "symfony-bundle", "description": "A bridge between Symfony and Parsley.js", "license": "Apache-2.0",