Skip to content

Commit

Permalink
Introduced a specific type for field triggering
Browse files Browse the repository at this point in the history
  • Loading branch information
c0ntax committed Feb 14, 2018
1 parent caf5e94 commit 7753603
Show file tree
Hide file tree
Showing 3 changed files with 104 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/Form/Extension/ParsleyTypeExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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) {
Expand Down
68 changes: 68 additions & 0 deletions src/Parsleys/Directive/Field/Trigger.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<?php
declare(strict_types = 1);

namespace C0ntax\ParsleyBundle\Parsleys\Directive\Field;

use C0ntax\ParsleyBundle\Contracts\DirectiveInterface;
use C0ntax\ParsleyBundle\Parsleys\Directive\Field\Constraint\AbstractConstraint;

/**
* Class Trigger
*
* @package C0ntax\ParsleyBundle\Parsleys\Directive\Field
*/
class Trigger implements DirectiveInterface
{
/** @var string */
private $key = 'trigger';

/** @var string */
private $value;

/**
* Trigger constructor.
*
* @param string $value
*/
public function __construct(string $value)
{
$this->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;
}
}
34 changes: 34 additions & 0 deletions tests/Parsleys/Directive/Field/TriggerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

namespace C0ntax\ParsleyBundle\Tests\Parsleys\Directive\Field;

use C0ntax\ParsleyBundle\Parsleys\Directive\Field\Trigger;

class TriggerTest extends \PHPUnit_Framework_TestCase
{
/**
* @dataProvider createGetViewAttrTestData
*/
public function testGetViewAttr(string $value, array $expected)
{
$trigger = new Trigger($value);
self::assertEquals($expected, $trigger->getViewAttr());
}

/**
* @return array
*/
public function createGetViewAttrTestData(): array
{
return [
[
'value' => 'click',
'expected' => ['data-parsley-trigger' => 'click'],
],
[
'value' => 'change focus',
'expected' => ['data-parsley-trigger' => 'change focus'],
],
];
}
}

0 comments on commit 7753603

Please sign in to comment.