Skip to content

Commit

Permalink
Merge branch 'release/0.6.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
c0ntax committed Feb 14, 2018
2 parents 01192bd + 1785cb7 commit 4313f5a
Show file tree
Hide file tree
Showing 7 changed files with 203 additions and 8 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
21 changes: 14 additions & 7 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 @@ -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 Generic('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) {
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;
}
}
27 changes: 27 additions & 0 deletions tests/Fixtures/Entity/TriggerEntity.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

namespace C0ntax\ParsleyBundle\Tests\Fixtures\Entity;

class TriggerEntity
{
private $check = false;

/**
* @return bool
*/
public function isCheck(): bool
{
return $this->check;
}

/**
* @param bool $check
* @return TriggerEntity
*/
public function setCheck(bool $check)
{
$this->check = $check;

return $this;
}
}
35 changes: 35 additions & 0 deletions tests/Fixtures/Form/Type/TestTriggerType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

namespace C0ntax\ParsleyBundle\Tests\Fixtures\Form\Type;

use C0ntax\ParsleyBundle\Parsleys\Directive\Field\Constraint\Required;
use C0ntax\ParsleyBundle\Parsleys\Directive\Field\Trigger;
use C0ntax\ParsleyBundle\Tests\Fixtures\Entity\TriggerEntity;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\CheckboxType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;

class TestTriggerType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add(
'check',
CheckboxType::class,
[
'parsleys' => [
new Required(),
new Trigger('click')
],
]
)
;
}

public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults(['data_class' => TriggerEntity::class]);
}
}
24 changes: 24 additions & 0 deletions tests/Form/Extension/ParsleyTypeExtensionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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 [
Expand Down Expand Up @@ -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);
}

}
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 4313f5a

Please sign in to comment.