1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace JBen87\ParsleyBundle\Form\Extension; |
4
|
|
|
|
5
|
|
|
use JBen87\ParsleyBundle\Constraint\Factory\FactoryRegistry; |
6
|
|
|
use JBen87\ParsleyBundle\Constraint\Reader\ReaderRegistry; |
7
|
|
|
use JBen87\ParsleyBundle\Exception\ConstraintException; |
8
|
|
|
use Psr\Log\LoggerInterface; |
9
|
|
|
use Symfony\Component\Form\AbstractTypeExtension; |
10
|
|
|
use Symfony\Component\Form\Extension\Core\Type\FormType; |
11
|
|
|
use Symfony\Component\Form\FormInterface; |
12
|
|
|
use Symfony\Component\Form\FormView; |
13
|
|
|
use Symfony\Component\OptionsResolver\OptionsResolver; |
14
|
|
|
use Symfony\Component\Serializer\Normalizer\NormalizerInterface; |
15
|
|
|
use Symfony\Component\Validator\Constraint as SymfonyConstraint; |
16
|
|
|
|
17
|
|
|
class ParsleyTypeExtension extends AbstractTypeExtension |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* @var FactoryRegistry |
21
|
|
|
*/ |
22
|
|
|
private $factoryRegistry; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var LoggerInterface |
26
|
|
|
*/ |
27
|
|
|
private $logger; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var NormalizerInterface |
31
|
|
|
*/ |
32
|
|
|
private $normalizer; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @var ReaderRegistry |
36
|
|
|
*/ |
37
|
|
|
private $readerRegistry; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @var bool |
41
|
|
|
*/ |
42
|
|
|
private $enabled; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @var string |
46
|
|
|
*/ |
47
|
|
|
private $triggerEvent; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @param FactoryRegistry $factoryRegistry |
51
|
|
|
* @param LoggerInterface $logger |
52
|
|
|
* @param NormalizerInterface $normalizer |
53
|
|
|
* @param ReaderRegistry $readerRegistry |
54
|
|
|
* @param bool $enabled |
55
|
|
|
* @param string $triggerEvent |
56
|
|
|
*/ |
57
|
8 |
|
public function __construct( |
58
|
|
|
FactoryRegistry $factoryRegistry, |
59
|
|
|
LoggerInterface $logger, |
60
|
|
|
NormalizerInterface $normalizer, |
61
|
|
|
ReaderRegistry $readerRegistry, |
62
|
|
|
bool $enabled, |
63
|
|
|
string $triggerEvent |
64
|
|
|
) { |
65
|
8 |
|
$this->factoryRegistry = $factoryRegistry; |
66
|
8 |
|
$this->logger = $logger; |
67
|
8 |
|
$this->normalizer = $normalizer; |
68
|
8 |
|
$this->readerRegistry = $readerRegistry; |
69
|
8 |
|
$this->enabled = $enabled; |
70
|
8 |
|
$this->triggerEvent = $triggerEvent; |
71
|
8 |
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @inheritdoc |
75
|
|
|
*/ |
76
|
7 |
|
public function finishView(FormView $view, FormInterface $form, array $options): void |
77
|
|
|
{ |
78
|
7 |
|
if (false === $options['parsley_enabled']) { |
79
|
2 |
|
return; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
// enable parsley validation on root form |
83
|
5 |
|
if (true === $form->isRoot()) { |
84
|
1 |
|
$view->vars['attr'] += [ |
85
|
|
|
'novalidate' => true, |
86
|
|
|
'data-parsley-validate' => true, |
87
|
|
|
]; |
88
|
|
|
|
89
|
1 |
|
return; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
// set trigger event attribute on form children |
93
|
4 |
|
$view->vars['attr']['data-parsley-trigger'] = $options['parsley_trigger_event']; |
94
|
|
|
|
95
|
|
|
// build constraints and map them as data attributes |
96
|
4 |
|
foreach ($this->getConstraints($form) as $symfonyConstraint) { |
97
|
|
|
try { |
98
|
2 |
|
$factory = $this->factoryRegistry->findForConstraint($symfonyConstraint); |
99
|
1 |
|
$constraint = $factory->create($symfonyConstraint); |
100
|
|
|
|
101
|
1 |
|
$view->vars['attr'] = array_merge($view->vars['attr'], $constraint->normalize($this->normalizer)); |
102
|
1 |
|
} catch (ConstraintException $exception) { |
103
|
2 |
|
$this->logger->warning($exception->getMessage(), ['constraint' => $symfonyConstraint]); |
104
|
|
|
} |
105
|
|
|
} |
106
|
4 |
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* @inheritdoc |
110
|
|
|
*/ |
111
|
8 |
|
public function configureOptions(OptionsResolver $resolver): void |
112
|
|
|
{ |
113
|
8 |
|
$resolver->setDefaults([ |
114
|
8 |
|
'parsley_enabled' => $this->enabled, |
115
|
8 |
|
'parsley_trigger_event' => $this->triggerEvent, |
116
|
|
|
]); |
117
|
8 |
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* @inheritdoc |
121
|
|
|
*/ |
122
|
1 |
|
public static function getExtendedTypes(): iterable |
123
|
|
|
{ |
124
|
1 |
|
yield FormType::class; |
125
|
1 |
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* @param FormInterface $form |
129
|
|
|
* |
130
|
|
|
* @return SymfonyConstraint[] |
131
|
|
|
*/ |
132
|
4 |
|
private function getConstraints(FormInterface $form): array |
133
|
|
|
{ |
134
|
4 |
|
$constraints = []; |
135
|
4 |
|
foreach ($this->readerRegistry->all() as $reader) { |
136
|
3 |
|
$constraints = array_merge($constraints, $reader->read($form)); |
137
|
|
|
} |
138
|
|
|
|
139
|
4 |
|
return $constraints; |
140
|
|
|
} |
141
|
|
|
} |
142
|
|
|
|