@@ -28,6 +28,11 @@ class FormElementErrors extends AbstractHelper
28
28
*/
29
29
protected $ attributes = [];
30
30
31
+ /**
32
+ * @var bool Whether or not to translate error messages during render.
33
+ */
34
+ protected $ translateErrorMessages = true ;
35
+
31
36
/**
32
37
* Invoke helper as functor
33
38
*
@@ -49,6 +54,10 @@ public function __invoke(ElementInterface $element = null, array $attributes = [
49
54
/**
50
55
* Render validation errors for the provided $element
51
56
*
57
+ * If {@link $translateErrorMessages} is true, and a translator is
58
+ * composed, messages retrieved from the element will be translated; if
59
+ * either is not the case, they will not.
60
+ *
52
61
* @param ElementInterface $element
53
62
* @param array $attributes
54
63
* @throws Exception\DomainException
@@ -60,39 +69,32 @@ public function render(ElementInterface $element, array $attributes = [])
60
69
if (empty ($ messages )) {
61
70
return '' ;
62
71
}
63
- if (! is_array ($ messages ) && ! $ messages instanceof Traversable) {
72
+
73
+ $ messages = $ messages instanceof Traversable ? iterator_to_array ($ messages ) : $ messages ;
74
+ if (! is_array ($ messages )) {
64
75
throw new Exception \DomainException (sprintf (
65
76
'%s expects that $element->getMessages() will return an array or Traversable; received "%s" ' ,
66
77
__METHOD__ ,
67
78
(is_object ($ messages ) ? get_class ($ messages ) : gettype ($ messages ))
68
79
));
69
80
}
70
81
82
+ // Flatten message array
83
+ $ messages = $ this ->flattenMessages ($ messages );
84
+ if (empty ($ messages )) {
85
+ return '' ;
86
+ }
87
+
71
88
// Prepare attributes for opening tag
72
89
$ attributes = array_merge ($ this ->attributes , $ attributes );
73
90
$ attributes = $ this ->createAttributesString ($ attributes );
74
91
if (! empty ($ attributes )) {
75
92
$ attributes = ' ' . $ attributes ;
76
93
}
77
94
78
- // Flatten message array
79
- $ escapeHtml = $ this ->getEscapeHtmlHelper ();
80
- $ messagesToPrint = [];
81
- $ translator = $ this ->getTranslator ();
82
- $ textDomain = $ this ->getTranslatorTextDomain ();
83
- $ messageCallback = function ($ item ) use (&$ messagesToPrint , $ escapeHtml , $ translator , $ textDomain ) {
84
- $ item = $ translator ? $ translator ->translate ($ item , $ textDomain ) : $ item ;
85
- $ messagesToPrint [] = $ escapeHtml ($ item );
86
- };
87
- array_walk_recursive ($ messages , $ messageCallback );
88
-
89
- if (empty ($ messagesToPrint )) {
90
- return '' ;
91
- }
92
-
93
95
// Generate markup
94
96
$ markup = sprintf ($ this ->getMessageOpenFormat (), $ attributes );
95
- $ markup .= implode ($ this ->getMessageSeparatorString (), $ messagesToPrint );
97
+ $ markup .= implode ($ this ->getMessageSeparatorString (), $ messages );
96
98
$ markup .= $ this ->getMessageCloseString ();
97
99
98
100
return $ markup ;
@@ -185,4 +187,56 @@ public function getMessageSeparatorString()
185
187
{
186
188
return $ this ->messageSeparatorString ;
187
189
}
190
+
191
+ /**
192
+ * Set the flag detailing whether or not to translate error messages.
193
+ *
194
+ * @param bool $flag
195
+ * @return self
196
+ */
197
+ public function setTranslateMessages ($ flag )
198
+ {
199
+ $ this ->translateErrorMessages = (bool ) $ flag ;
200
+ return $ this ;
201
+ }
202
+
203
+ /**
204
+ * @param array $messages
205
+ * @return array
206
+ */
207
+ private function flattenMessages (array $ messages )
208
+ {
209
+ return $ this ->translateErrorMessages && $ this ->getTranslator ()
210
+ ? $ this ->flattenMessagesWithTranslator ($ messages )
211
+ : $ this ->flattenMessagesWithoutTranslator ($ messages );
212
+ }
213
+
214
+ /**
215
+ * @param array $messages
216
+ * @return array
217
+ */
218
+ private function flattenMessagesWithoutTranslator (array $ messages )
219
+ {
220
+ $ messagesToPrint = [];
221
+ array_walk_recursive ($ messages , function ($ item ) use (&$ messagesToPrint ) {
222
+ $ messagesToPrint [] = $ item ;
223
+ });
224
+ return $ messagesToPrint ;
225
+ }
226
+
227
+ /**
228
+ * @param array $messages
229
+ * @return array
230
+ */
231
+ private function flattenMessagesWithTranslator (array $ messages )
232
+ {
233
+ $ translator = $ this ->getTranslator ();
234
+ $ textDomain = $ this ->getTranslatorTextDomain ();
235
+ $ messagesToPrint = [];
236
+ $ messageCallback = function ($ item ) use (&$ messagesToPrint , $ translator , $ textDomain ) {
237
+ $ messagesToPrint [] = $ translator ->translate ($ item , $ textDomain );
238
+ };
239
+ array_walk_recursive ($ messages , $ messageCallback );
240
+ return $ messagesToPrint ;
241
+ }
188
242
}
0 commit comments