Skip to content

Commit

Permalink
Merge branch 'release/0.6.2'
Browse files Browse the repository at this point in the history
  • Loading branch information
c0ntax committed Feb 14, 2018
2 parents 4b1665c + 84ff21d commit f45d742
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 12 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.6.1",
"version": "0.6.2",
"type": "symfony-bundle",
"description": "A bridge between Symfony and Parsley.js",
"license": "Apache-2.0",
Expand Down
44 changes: 33 additions & 11 deletions src/Form/Extension/ParsleyTypeExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,15 +68,19 @@ public function buildView(FormView $view, FormInterface $form, array $options):
$this->getRemoveSymfonyConstraintsFromParsleys($parsleys)
);

$parsleyConstraints = $this->removeFromConstraints(
array_merge(
$this->createParsleyConstraintsFromValidationConstraints($symfonyConstraints, $form),
$this->getDirectivesFromParsleys($parsleys)
),
$parsleyConstraints = array_merge(
$this->createParsleyConstraintsFromValidationConstraints($symfonyConstraints, $form),
$this->getDirectivesFromParsleys($parsleys)
);

$parsleyDirectives = $this->addDefaultDirectives($parsleyConstraints);

$parsleyDirectives = $this->removeFromConstraints(
$parsleyDirectives,
$this->getRemoveParsleyDirectivesFromParsleys($parsleys)
);

$this->addParsleyToView($view, $parsleyConstraints);
$this->addParsleyToView($view, $parsleyDirectives);
}

/**
Expand All @@ -103,6 +107,29 @@ public function configureOptions(OptionsResolver $resolver): void
->addAllowedTypes(self::OPTION_NAME, 'array');
}

/**
* @param array $parsleyDirectives
* @return array
*/
private function addDefaultDirectives(array $parsleyDirectives): array
{
$has = [];
foreach ($parsleyDirectives as $parsleyDirective) {
$class = get_class($parsleyDirective);
if (array_key_exists($class, $has)) {
$has[$class]++;
} else {
$has[$class] = 1;
}
}

if (!array_key_exists(Trigger::class, $has) && count($parsleyDirectives) > 0 && $this->getConfig()['field']['trigger'] !== null) {
$parsleyDirectives[] = new Trigger($this->getConfig()['field']['trigger']);
}

return $parsleyDirectives;
}

/**
* @param ParsleyInterface[] $parsleys
* @return DirectiveInterface[]
Expand Down Expand Up @@ -230,11 +257,6 @@ private function addParsleyToView(FormView $view, array $directives): void
}
}

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) {
$view->vars['attr'] = array_merge($view->vars['attr'], $attr);
}
Expand Down

0 comments on commit f45d742

Please sign in to comment.