Skip to content

Commit 4313f5a

Browse files
committed
Merge branch 'release/0.6.0'
2 parents 01192bd + 1785cb7 commit 4313f5a

7 files changed

Lines changed: 203 additions & 8 deletions

File tree

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "c0ntax/parsley-bundle",
3-
"version": "0.5.0",
3+
"version": "0.6.0",
44
"type": "symfony-bundle",
55
"description": "A bridge between Symfony and Parsley.js",
66
"license": "Apache-2.0",

src/Form/Extension/ParsleyTypeExtension.php

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
use C0ntax\ParsleyBundle\Contracts\ParsleyInterface;
99
use C0ntax\ParsleyBundle\Contracts\RemoveInterface;
1010
use C0ntax\ParsleyBundle\Factory\ConstraintFactory;
11-
use C0ntax\ParsleyBundle\Parsleys\Directive\Field\Generic;
11+
use C0ntax\ParsleyBundle\Parsleys\Directive\Field\Trigger;
1212
use C0ntax\ParsleyBundle\Parsleys\RemoveParsleyConstraint;
1313
use C0ntax\ParsleyBundle\Parsleys\RemoveSymfonyConstraint;
1414
use Symfony\Component\Form\AbstractTypeExtension;
@@ -214,16 +214,23 @@ private function getConstraintsFromForm(FormInterface $form): array
214214
* @param FormView $view
215215
* @param DirectiveInterface[] $directives
216216
*/
217-
private function addParsleyToView(FormView $view, array $directives)
217+
private function addParsleyToView(FormView $view, array $directives): void
218218
{
219219
$attr = [];
220-
if (count($directives) > 0 && $this->getConfig()['field']['trigger'] !== null) {
221-
$directives[] = new Generic('trigger', $this->getConfig()['field']['trigger']);
222-
}
223-
foreach ($directives as $constraint) {
224-
foreach ($constraint->getViewAttr() as $key => $value) {
220+
$hasTrigger = false;
221+
222+
foreach ($directives as $directive) {
223+
foreach ($directive->getViewAttr() as $key => $value) {
225224
$attr[$key] = $value;
226225
}
226+
if ($directive instanceof Trigger) {
227+
$hasTrigger = true;
228+
}
229+
}
230+
231+
if (!$hasTrigger && count($directives) > 0 && $this->getConfig()['field']['trigger'] !== null) {
232+
$trigger = new Trigger($this->getConfig()['field']['trigger']);
233+
$attr = array_merge($attr, $trigger->getViewAttr());
227234
}
228235

229236
if (count($attr) > 0) {
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
<?php
2+
declare(strict_types = 1);
3+
4+
namespace C0ntax\ParsleyBundle\Parsleys\Directive\Field;
5+
6+
use C0ntax\ParsleyBundle\Contracts\DirectiveInterface;
7+
use C0ntax\ParsleyBundle\Parsleys\Directive\Field\Constraint\AbstractConstraint;
8+
9+
/**
10+
* Class Trigger
11+
*
12+
* @package C0ntax\ParsleyBundle\Parsleys\Directive\Field
13+
*/
14+
class Trigger implements DirectiveInterface
15+
{
16+
/** @var string */
17+
private $key = 'trigger';
18+
19+
/** @var string */
20+
private $value;
21+
22+
/**
23+
* Trigger constructor.
24+
*
25+
* @param string $value
26+
*/
27+
public function __construct(string $value)
28+
{
29+
$this->setValue($value);
30+
}
31+
32+
/**
33+
* @return array
34+
*/
35+
public function getViewAttr(): array
36+
{
37+
return [
38+
implode('-', [AbstractConstraint::DATA_ATTRIBUTE_PREFIX, $this->getKey()]) => $this->getValue(),
39+
];
40+
}
41+
42+
/**
43+
* @return string
44+
*/
45+
private function getKey(): string
46+
{
47+
return $this->key;
48+
}
49+
50+
/**
51+
* @return string
52+
*/
53+
private function getValue(): string
54+
{
55+
return $this->value;
56+
}
57+
58+
/**
59+
* @param string $value
60+
* @return Trigger
61+
*/
62+
private function setValue(string $value): Trigger
63+
{
64+
$this->value = $value;
65+
66+
return $this;
67+
}
68+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
namespace C0ntax\ParsleyBundle\Tests\Fixtures\Entity;
4+
5+
class TriggerEntity
6+
{
7+
private $check = false;
8+
9+
/**
10+
* @return bool
11+
*/
12+
public function isCheck(): bool
13+
{
14+
return $this->check;
15+
}
16+
17+
/**
18+
* @param bool $check
19+
* @return TriggerEntity
20+
*/
21+
public function setCheck(bool $check)
22+
{
23+
$this->check = $check;
24+
25+
return $this;
26+
}
27+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
3+
namespace C0ntax\ParsleyBundle\Tests\Fixtures\Form\Type;
4+
5+
use C0ntax\ParsleyBundle\Parsleys\Directive\Field\Constraint\Required;
6+
use C0ntax\ParsleyBundle\Parsleys\Directive\Field\Trigger;
7+
use C0ntax\ParsleyBundle\Tests\Fixtures\Entity\TriggerEntity;
8+
use Symfony\Component\Form\AbstractType;
9+
use Symfony\Component\Form\Extension\Core\Type\CheckboxType;
10+
use Symfony\Component\Form\FormBuilderInterface;
11+
use Symfony\Component\OptionsResolver\OptionsResolver;
12+
13+
class TestTriggerType extends AbstractType
14+
{
15+
public function buildForm(FormBuilderInterface $builder, array $options)
16+
{
17+
$builder
18+
->add(
19+
'check',
20+
CheckboxType::class,
21+
[
22+
'parsleys' => [
23+
new Required(),
24+
new Trigger('click')
25+
],
26+
]
27+
)
28+
;
29+
}
30+
31+
public function configureOptions(OptionsResolver $resolver)
32+
{
33+
$resolver->setDefaults(['data_class' => TriggerEntity::class]);
34+
}
35+
}

tests/Form/Extension/ParsleyTypeExtensionTest.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace C0ntax\ParsleyBundle\Tests\Form\Extension;
44

55
use C0ntax\ParsleyBundle\Tests\Fixtures\Form\Type\TestRemovalType;
6+
use C0ntax\ParsleyBundle\Tests\Fixtures\Form\Type\TestTriggerType;
67
use C0ntax\ParsleyBundle\Tests\Fixtures\Form\Type\TestType;
78
use C0ntax\ParsleyBundle\Tests\Form\AbstractTypeTestCase;
89
use Symfony\Component\Form\FormInterface;
@@ -70,6 +71,20 @@ public function testRemoval()
7071
);
7172
}
7273

74+
public function testTriggerOverride()
75+
{
76+
$form = $this->createTriggerForm();
77+
$view = $form->createView();
78+
self::assertEquals(
79+
[
80+
'data-parsley-required' => 'true',
81+
'data-parsley-trigger' => 'click',
82+
],
83+
$view->children['check']->vars['attr']
84+
);
85+
86+
}
87+
7388
protected function getParsleyTypeConfig()
7489
{
7590
return [
@@ -98,4 +113,13 @@ private function createRemovalForm(): FormInterface
98113
return $this->factory->create(TestRemovalType::class);
99114
}
100115

116+
/**
117+
* @return FormInterface
118+
* @throws \Symfony\Component\OptionsResolver\Exception\InvalidOptionsException
119+
*/
120+
private function createTriggerForm(): FormInterface
121+
{
122+
return $this->factory->create(TestTriggerType::class);
123+
}
124+
101125
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
3+
namespace C0ntax\ParsleyBundle\Tests\Parsleys\Directive\Field;
4+
5+
use C0ntax\ParsleyBundle\Parsleys\Directive\Field\Trigger;
6+
7+
class TriggerTest extends \PHPUnit_Framework_TestCase
8+
{
9+
/**
10+
* @dataProvider createGetViewAttrTestData
11+
*/
12+
public function testGetViewAttr(string $value, array $expected)
13+
{
14+
$trigger = new Trigger($value);
15+
self::assertEquals($expected, $trigger->getViewAttr());
16+
}
17+
18+
/**
19+
* @return array
20+
*/
21+
public function createGetViewAttrTestData(): array
22+
{
23+
return [
24+
[
25+
'value' => 'click',
26+
'expected' => ['data-parsley-trigger' => 'click'],
27+
],
28+
[
29+
'value' => 'change focus',
30+
'expected' => ['data-parsley-trigger' => 'change focus'],
31+
],
32+
];
33+
}
34+
}

0 commit comments

Comments
 (0)