Skip to content

Commit

Permalink
If a trigger is specified at a form level, if there is a default from…
Browse files Browse the repository at this point in the history
… the configuration, we ignore it
  • Loading branch information
c0ntax committed Feb 14, 2018
1 parent 7753603 commit f7eb772
Show file tree
Hide file tree
Showing 4 changed files with 99 additions and 6 deletions.
19 changes: 13 additions & 6 deletions src/Form/Extension/ParsleyTypeExtension.php
Original file line number Diff line number Diff line change
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 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
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);
}

}

0 comments on commit f7eb772

Please sign in to comment.