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'], + ], + ]; + } +}