1
|
|
|
<?php |
2
|
|
|
namespace Boekkooi\Bundle\JqueryValidationBundle\Form\Rule\Processor; |
3
|
|
|
|
4
|
|
|
use Boekkooi\Bundle\JqueryValidationBundle\Form\FormRuleContextBuilder; |
5
|
|
|
use Boekkooi\Bundle\JqueryValidationBundle\Form\FormRuleProcessorContext; |
6
|
|
|
use Boekkooi\Bundle\JqueryValidationBundle\Form\FormRuleProcessorInterface; |
7
|
|
|
use Boekkooi\Bundle\JqueryValidationBundle\Form\Rule\ConstraintRule; |
8
|
|
|
use Boekkooi\Bundle\JqueryValidationBundle\Form\Util\FormViewRecursiveIterator; |
9
|
|
|
use Boekkooi\Bundle\JqueryValidationBundle\Validator\ConstraintCollection; |
10
|
|
|
use Symfony\Component\Form\FormInterface; |
11
|
|
|
use Symfony\Component\Form\FormView; |
12
|
|
|
use Symfony\Component\Validator\Constraints\Valid; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* @author Warnar Boekkooi <[email protected]> |
16
|
|
|
*/ |
17
|
|
|
class ValidConstraintPass implements FormRuleProcessorInterface |
18
|
|
|
{ |
19
|
|
|
public function process(FormRuleProcessorContext $processContext, FormRuleContextBuilder $formRuleContext) |
20
|
|
|
{ |
21
|
|
|
$form = $processContext->getForm(); |
22
|
|
|
if (!$this->requiresValidConstraint($form) || $this->hasValidConstraint($processContext->getConstraints())) { |
23
|
|
|
return; |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
$view = $processContext->getView(); |
27
|
|
|
$it = new \RecursiveIteratorIterator( |
28
|
|
|
new FormViewRecursiveIterator($view->getIterator()), |
29
|
|
|
\RecursiveIteratorIterator::SELF_FIRST |
30
|
|
|
); |
31
|
|
|
foreach ($it as $childView) { |
32
|
|
|
if (isset($childView->vars['required'])) { |
33
|
|
|
$childView->vars['required'] = false; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
$this->cleanChildRules($childView, $formRuleContext); |
37
|
|
|
} |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
private function requiresValidConstraint(FormInterface $form) |
41
|
|
|
{ |
42
|
|
|
$formConfig = $form->getConfig(); |
43
|
|
|
|
44
|
|
|
return !$form->isRoot() && |
45
|
|
|
$formConfig->getCompound() && |
46
|
|
|
$formConfig->getMapped() && |
47
|
|
|
$formConfig->getDataClass() !== null |
48
|
|
|
; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
private function hasValidConstraint(ConstraintCollection $constraints) |
52
|
|
|
{ |
53
|
|
|
foreach ($constraints as $constraint) { |
54
|
|
|
if (get_class($constraint) === Valid::class) { |
55
|
|
|
return true; |
56
|
|
|
} |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
return false; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
private function cleanChildRules(FormView $childView, FormRuleContextBuilder $formRuleContext) |
63
|
|
|
{ |
64
|
|
|
$rules = $formRuleContext->get($childView); |
65
|
|
|
if ($rules === null) { |
66
|
|
|
return; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
// Don't remove transformer rules! |
70
|
|
|
foreach ($rules as $name => $rule) { |
71
|
|
|
if (!$rule instanceof ConstraintRule) { |
72
|
|
|
continue; |
73
|
|
|
} |
74
|
|
|
$rules->remove($name); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
if (empty($rules)) { |
78
|
|
|
$formRuleContext->remove($childView); |
79
|
|
|
} |
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
|