1
|
|
|
<?php |
2
|
|
|
namespace RomanTymoshyk\UnobtrusiveValidationBundle\Form\Extension; |
3
|
|
|
|
4
|
|
|
use Symfony\Component\Form\AbstractTypeExtension; |
5
|
|
|
use Symfony\Component\Form\FormInterface; |
6
|
|
|
use Symfony\Component\Form\FormView; |
7
|
|
|
use Symfony\Component\OptionsResolver\OptionsResolver; |
8
|
|
|
use Symfony\Component\Translation\TranslatorInterface; |
9
|
|
|
use Symfony\Component\Validator\Constraint; |
10
|
|
|
|
11
|
|
|
class UnobtrusiveValidationExtension extends AbstractTypeExtension |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* @var TranslatorInterface |
15
|
|
|
*/ |
16
|
|
|
protected $translator; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @var null|string |
20
|
|
|
*/ |
21
|
|
|
protected $translationDomain; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @param TranslatorInterface $translator The translator for translating error messages |
25
|
|
|
* @param null|string $translationDomain The translation domain for translating |
26
|
|
|
*/ |
27
|
|
|
public function __construct(TranslatorInterface $translator = null, $translationDomain = false) |
28
|
|
|
{ |
29
|
|
|
$this->translator = $translator; |
30
|
|
|
$this->translationDomain = $translationDomain; |
|
|
|
|
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* {@inheritdoc} |
35
|
|
|
*/ |
36
|
|
|
public function configureOptions(OptionsResolver $resolver) |
37
|
|
|
{ |
38
|
|
|
parent::configureOptions($resolver); |
39
|
|
|
|
40
|
|
|
$resolver->setDefaults( |
41
|
|
|
array( |
42
|
|
|
'label_translation_domain' => false, |
43
|
|
|
'translation_domain' => $this->translationDomain |
44
|
|
|
) |
45
|
|
|
) |
46
|
|
|
->setAllowedTypes('label_translation_domain', array('null', 'string', 'boolean')) |
47
|
|
|
->setAllowedTypes('translation_domain', array('null', 'string', 'boolean')); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* {@inheritdoc} |
52
|
|
|
*/ |
53
|
|
|
public function buildView(FormView $view, FormInterface $form, array $options) |
54
|
|
|
{ |
55
|
|
|
if (count($options['constraints']) > 0) { |
56
|
|
|
$view->vars['attr']['data-val'] = 'true'; |
57
|
|
|
|
58
|
|
|
$label = $this->trans($options['label'], array(), $options['label_translation_domain']); |
59
|
|
|
|
60
|
|
|
/** @var Constraint $constraint */ |
61
|
|
|
foreach ($options['constraints'] as $constraint) { |
62
|
|
|
switch (get_class($constraint)) { |
63
|
|
|
case 'Symfony\Component\Validator\Constraints\Required': |
64
|
|
|
$view->vars['attr']['data-val-required'] = $this->trans( |
65
|
|
|
'The \'{{ field_name }}\' field is required.', |
66
|
|
|
array('{{ field_name }}' => $label) |
67
|
|
|
); |
68
|
|
|
break; |
69
|
|
|
case 'Symfony\Component\Validator\Constraints\Regex': |
70
|
|
|
$view->vars['attr']['data-val-regex'] = $this->trans( |
71
|
|
|
'The \'{{ field_name }}\' value is not valid', |
72
|
|
|
array('{{ field_name }}' => $label) |
73
|
|
|
); |
74
|
|
|
$view->vars['attr']['data-val-regex-pattern'] = $constraint->htmlPattern; |
75
|
|
|
break; |
76
|
|
|
case 'Symfony\Component\Validator\Constraints\Range': |
77
|
|
|
if (!empty($constraint->min) && !empty($constraint->max)) { |
78
|
|
|
$view->vars['attr']['data-val-range'] = $this->trans( |
79
|
|
|
'The field \'{{ field_name }}\' value should be in range {{ min }} to {{ max }}.', |
80
|
|
|
array( |
81
|
|
|
'{{ field_name }}' => $label, |
82
|
|
|
'{{ min }}' => $constraint->min, |
83
|
|
|
'{{ max }}' => $constraint->max |
84
|
|
|
) |
85
|
|
|
); |
86
|
|
|
} |
87
|
|
|
break; |
88
|
|
|
case 'Symfony\Component\Validator\Constraints\LessThanOrEqual': |
89
|
|
|
if (!empty($constraint->min)) { |
90
|
|
|
$view->vars['attr']['data-val-length-max'] = $constraint->min; |
91
|
|
|
$view->vars['attr']['data-val-length'] = $this->trans( |
92
|
|
|
'The field \'{{ field_name }}\' should be less than {{ limit }}.', |
93
|
|
|
array( |
94
|
|
|
'{{ field_name }}' => $label, |
95
|
|
|
'{{ limit }}' => $constraint->min |
96
|
|
|
) |
97
|
|
|
); |
98
|
|
|
} |
99
|
|
|
break; |
100
|
|
|
case 'Symfony\Component\Validator\Constraints\GreaterThanOrEqual': |
101
|
|
|
if (!empty($constraint->min)) { |
102
|
|
|
$view->vars['attr']['data-val-length-min'] = $constraint->min; |
103
|
|
|
$view->vars['attr']['data-val-length'] = $this->trans( |
104
|
|
|
'The field \'{{ field_name }}\' should be greater than {{ limit }}.', |
105
|
|
|
array( |
106
|
|
|
'{{ field_name }}' => $label, |
107
|
|
|
'{{ limit }}' => $constraint->min |
108
|
|
|
) |
109
|
|
|
); |
110
|
|
|
} |
111
|
|
|
break; |
112
|
|
|
case 'Symfony\Component\Validator\Constraints\Length': |
113
|
|
|
if (!empty($constraint->min)) { |
114
|
|
|
$view->vars['attr']['data-val-length-min'] = $constraint->min; |
115
|
|
|
$view->vars['attr']['data-val-length'] = $this->trans( |
116
|
|
|
'The field \'{{ field_name }}\' should have {{ limit }} or more.', |
117
|
|
|
array( |
118
|
|
|
'{{ field_name }}' => $label, |
119
|
|
|
'{{ limit }}' => $constraint->min |
120
|
|
|
) |
121
|
|
|
); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
if (!empty($constraint->max)) { |
125
|
|
|
$view->vars['attr']['data-val-length-max'] = $constraint->max; |
126
|
|
|
$view->vars['attr']['data-val-length'] = $this->trans( |
127
|
|
|
'The field \'{{ field_name }}\' should have {{ limit }} or less.', |
128
|
|
|
array( |
129
|
|
|
'{{ field_name }}' => $label, |
130
|
|
|
'{{ limit }}' => $constraint->max |
131
|
|
|
) |
132
|
|
|
); |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
if (!empty($constraint->min) && !empty($constraint->max)) { |
136
|
|
|
$view->vars['attr']['data-val-length'] = $this->trans( |
137
|
|
|
'The field \'{{ field_name }}\' should have from {{ min }} to {{ max }} characters.', |
138
|
|
|
array( |
139
|
|
|
'{{ field_name }}' => $label, |
140
|
|
|
'{{ min }}' => $constraint->min, |
141
|
|
|
'{{ max }}' => $constraint->max |
142
|
|
|
) |
143
|
|
|
); |
144
|
|
|
} |
145
|
|
|
break; |
146
|
|
|
case 'Symfony\Component\Validator\Constraints\Type': |
147
|
|
|
switch ($constraint->type) { |
148
|
|
|
case 'integer': |
149
|
|
|
case 'int': |
150
|
|
|
case 'long': |
151
|
|
|
$view->vars['attr']['data-val-digits'] = $this->trans( |
152
|
|
|
'The \'{{ field_name }}\' field value is not a valid integer.', |
153
|
|
|
array('{{ field_name }}' => $label) |
154
|
|
|
); |
155
|
|
|
break; |
156
|
|
|
case 'float': |
157
|
|
|
case 'numeric': |
158
|
|
|
case 'real': |
159
|
|
|
$view->vars['attr']['data-val-number'] = $this->trans( |
160
|
|
|
'The \'{{ field_name }}\' field value is not a valid number.', |
161
|
|
|
array('{{ field_name }}' => $label) |
162
|
|
|
); |
163
|
|
|
break; |
164
|
|
|
} |
165
|
|
|
break; |
166
|
|
|
case 'Symfony\Component\Validator\Constraints\Date': |
167
|
|
|
$view->vars['attr']['data-val-date'] = $this->trans( |
168
|
|
|
'The \'{{ field_name }}\' field value is not a valid date.', |
169
|
|
|
array('{{ field_name }}' => $label) |
170
|
|
|
); |
171
|
|
|
break; |
172
|
|
|
case 'Symfony\Component\Validator\Constraints\Email': |
173
|
|
|
$view->vars['attr']['data-val-email'] = $this->trans( |
174
|
|
|
'The \'{{ field_name }}\' field value is not a valid email address.', |
175
|
|
|
array('{{ field_name }}' => $label) |
176
|
|
|
); |
177
|
|
|
break; |
178
|
|
|
case 'Symfony\Component\Validator\Constraints\CardScheme': |
179
|
|
|
$view->vars['attr']['data-val-creditcard'] = $this->trans( |
180
|
|
|
'The \'{{ field_name }}\' field value is not a valid credit card number.', |
181
|
|
|
array('{{ field_name }}' => $label) |
182
|
|
|
); |
183
|
|
|
break; |
184
|
|
|
case 'Symfony\Component\Validator\Constraints\Url': |
185
|
|
|
$view->vars['attr']['data-val-url'] = $this->trans( |
186
|
|
|
'The \'{{ field_name }}\' field value is not a valid url.', |
187
|
|
|
array('{{ field_name }}' => $label) |
188
|
|
|
); |
189
|
|
|
break; |
190
|
|
|
} |
191
|
|
|
} |
192
|
|
|
} |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
/** |
196
|
|
|
* {@inheritdoc} |
197
|
|
|
*/ |
198
|
|
|
public function getExtendedType() |
199
|
|
|
{ |
200
|
|
|
return 'Symfony\Component\Form\Extension\Core\Type\FormType'; |
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
/** |
204
|
|
|
* Translates the given message. |
205
|
|
|
* |
206
|
|
|
* @param string $id The message id (may also be an object that can be cast to string) |
207
|
|
|
* @param array $parameters An array of parameters for the message |
208
|
|
|
* |
209
|
|
|
* @return string The translated string |
210
|
|
|
*/ |
211
|
|
|
public function trans($id, array $parameters = array(), $translationDomain = null) |
212
|
|
|
{ |
213
|
|
|
$result = $id; |
214
|
|
|
|
215
|
|
|
if ($this->translator !== null) { |
216
|
|
|
$translationDomain = $translationDomain !== null ? $translationDomain : $this->translationDomain; |
217
|
|
|
|
218
|
|
|
$result = $this->translator->trans( |
219
|
|
|
$id, |
220
|
|
|
$parameters, |
221
|
|
|
$translationDomain |
222
|
|
|
); |
223
|
|
|
} |
224
|
|
|
|
225
|
|
|
return $result; |
226
|
|
|
} |
227
|
|
|
} |
228
|
|
|
|
Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.
For example, imagine you have a variable
$accountId
that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to theid
property of an instance of theAccount
class. This class holds a proper account, so the id value must no longer be false.Either this assignment is in error or a type check should be added for that assignment.