Skip to content

Commit a4bf10a

Browse files
committed
fix: translate field label in error messages
1 parent 261e536 commit a4bf10a

20 files changed

+75
-55
lines changed

inc/abstractfield.class.php

+11
Original file line numberDiff line numberDiff line change
@@ -332,4 +332,15 @@ public function getTranslatableStrings(array $options = []) : array {
332332

333333
return $strings;
334334
}
335+
336+
/**
337+
* Translates the label of the field into the current language
338+
*
339+
* @return string
340+
*/
341+
protected function getTtranslatedLabel(): string {
342+
$form = PluginFormcreatorForm::getByItem($this->question);
343+
$domain = PluginFormcreatorForm::getTranslationDomain($form->getID());
344+
return __($this->getLabel(), $domain);
345+
}
335346
}

inc/field/actorfield.class.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -254,7 +254,7 @@ public function isValid(): bool {
254254
// If the field is required it can't be empty
255255
if ($this->isRequired() && count($this->value) === 0) {
256256
Session::addMessageAfterRedirect(
257-
sprintf(__('A required field is empty: %s', 'formcreator'), $this->getLabel()),
257+
sprintf(__('A required field is empty: %s', 'formcreator'), $this->getTtranslatedLabel()),
258258
false,
259259
ERROR
260260
);
@@ -264,7 +264,7 @@ public function isValid(): bool {
264264
// If an item has been removed by sanitization, then the data is not valid
265265
if (count($sanitized) != count($this->value)) {
266266
Session::addMessageAfterRedirect(
267-
sprintf(__('Invalid value: %s', 'formcreator'), $this->getLabel()),
267+
sprintf(__('Invalid value: %s', 'formcreator'), $this->getTtranslatedLabel()),
268268
false,
269269
ERROR
270270
);
@@ -287,7 +287,7 @@ public function isValidValue($value): bool {
287287
$user = new User();
288288
if (!$user->getFromDB($item)) {
289289
Session::addMessageAfterRedirect(
290-
sprintf(__('User not found or invalid email address: %s', 'formcreator'), $this->getLabel()),
290+
sprintf(__('User not found or invalid email address: %s', 'formcreator'), $this->getTtranslatedLabel()),
291291
false,
292292
ERROR
293293
);

inc/field/checkboxesfield.class.php

+5-5
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ public function isValid(): bool {
187187
// If the field is required it can't be empty
188188
if ($this->isRequired() && count($value) <= 0) {
189189
Session::addMessageAfterRedirect(
190-
sprintf(__('A required field is empty: %s', 'formcreator'), $this->getLabel()),
190+
sprintf(__('A required field is empty: %s', 'formcreator'), $this->getTtranslatedLabel()),
191191
false,
192192
ERROR
193193
);
@@ -223,14 +223,14 @@ public function isValidValue($value): bool {
223223
$rangeMin = $parameters['range']->fields['range_min'];
224224
$rangeMax = $parameters['range']->fields['range_max'];
225225
if ($rangeMin > 0 && count($value) < $rangeMin) {
226-
$message = sprintf(__('The following question needs at least %d answers', 'formcreator'), $rangeMin);
227-
Session::addMessageAfterRedirect($message . ' ' . $this->getLabel(), false, ERROR);
226+
$message = sprintf(__('The following question needs at least %d answers: %s', 'formcreator'), $rangeMin, $this->getTtranslatedLabel());
227+
Session::addMessageAfterRedirect($message, false, ERROR);
228228
return false;
229229
}
230230

231231
if ($rangeMax > 0 && count($value) > $rangeMax) {
232-
$message = sprintf(__('The following question does not accept more than %d answers', 'formcreator'), $rangeMax);
233-
Session::addMessageAfterRedirect($message . ' ' . $this->getLabel(), false, ERROR);
232+
$message = sprintf(__('The following question does not accept more than %d answers: %s', 'formcreator'), $rangeMax, $this->getTtranslatedLabel());
233+
Session::addMessageAfterRedirect($message, false, ERROR);
234234
return false;
235235
}
236236
}

inc/field/datefield.class.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ public function isValid(): bool {
112112
// If the field is required it can't be empty
113113
if ($this->isRequired() && (strtotime($this->value) == '')) {
114114
Session::addMessageAfterRedirect(
115-
sprintf(__('A required field is empty: %s', 'formcreator'), $this->getLabel()),
115+
sprintf(__('A required field is empty: %s', 'formcreator'), $this->getTtranslatedLabel()),
116116
false,
117117
ERROR
118118
);

inc/field/datetimefield.class.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ public function isValid(): bool {
115115
// If the field is required it can't be empty
116116
if ($this->isRequired() && (strtotime($this->value) == '')) {
117117
Session::addMessageAfterRedirect(
118-
sprintf(__('A required field is empty: %s', 'formcreator'), $this->getLabel()),
118+
sprintf(__('A required field is empty: %s', 'formcreator'), $this->getTtranslatedLabel()),
119119
false,
120120
ERROR
121121
);

inc/field/dropdownfield.class.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -438,7 +438,7 @@ public function isValid(): bool {
438438
$dropdown = new $itemtype();
439439
if ($this->isRequired() && $dropdown->isNewId($this->value)) {
440440
Session::addMessageAfterRedirect(
441-
__('A required field is empty:', 'formcreator') . ' ' . $this->getLabel(),
441+
__('A required field is empty:', 'formcreator') . ' ' . $this->getTtranslatedLabel(),
442442
false,
443443
ERROR
444444
);
@@ -460,7 +460,7 @@ public function isValidValue($value): bool {
460460

461461
if (!$isValid) {
462462
Session::addMessageAfterRedirect(
463-
__('Invalid value for ', 'formcreator') . ' ' . $this->getLabel(),
463+
__('Invalid value for ', 'formcreator') . ' ' . $this->getTtranslatedLabel(),
464464
false,
465465
ERROR
466466
);

inc/field/emailfield.class.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ public function isValidValue($value): bool {
8585

8686
if (!filter_var($value, FILTER_VALIDATE_EMAIL)) {
8787
Session::addMessageAfterRedirect(
88-
sprintf(__('This is not a valid e-mail: %s', 'formcreator'), $this->getLabel()),
88+
sprintf(__('This is not a valid e-mail: %s', 'formcreator'), $this->getTtranslatedLabel()),
8989
false,
9090
ERROR
9191
);

inc/field/filefield.class.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ public function isValid(): bool {
171171
$key = '_formcreator_field_' . $this->question->getID();
172172
if (($this->isRequired() && (!isset($this->uploads[$key]) || count($this->uploads[$key]) < 1))) {
173173
Session::addMessageAfterRedirect(
174-
sprintf(__('A required file is missing: %s', 'formcreator'), $this->getLabel()),
174+
sprintf(__('A required file is missing: %s', 'formcreator'), $this->getTtranslatedLabel()),
175175
false,
176176
ERROR
177177
);

inc/field/floatfield.class.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ public function getDocumentsForTarget(): array {
121121
public function isValid(): bool {
122122
if ($this->isRequired() && $this->value == '') {
123123
Session::addMessageAfterRedirect(
124-
sprintf(__('A required field is empty: %s', 'formcreator'), $this->getLabel()),
124+
sprintf(__('A required field is empty: %s', 'formcreator'), $this->getTtranslatedLabel()),
125125
false,
126126
ERROR
127127
);
@@ -138,7 +138,7 @@ public function isValidValue($value): bool {
138138

139139
if (!empty($value) && !is_numeric($value)) {
140140
Session::addMessageAfterRedirect(
141-
sprintf(__('This is not a number: %s', 'formcreator'), $this->getLabel()),
141+
sprintf(__('This is not a number: %s', 'formcreator'), $this->getTtranslatedLabel()),
142142
false,
143143
ERROR
144144
);

inc/field/integerfield.class.php

+4-4
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ public function isValidValue($value): bool {
5656
}
5757

5858
if (!empty($value) && !ctype_digit((string) $value)) {
59-
Session::addMessageAfterRedirect(sprintf(__('This is not an integer: %s', 'formcreator'), $this->getLabel()), false, ERROR);
59+
Session::addMessageAfterRedirect(sprintf(__('This is not an integer: %s', 'formcreator'), $this->getTtranslatedLabel()), false, ERROR);
6060
return false;
6161
}
6262

@@ -68,7 +68,7 @@ public function isValidValue($value): bool {
6868
if ($regex !== null && strlen($regex) > 0) {
6969
if (!preg_match($regex, $value)) {
7070
Session::addMessageAfterRedirect(
71-
sprintf(__('Specific format does not match: %s', 'formcreator'), $this->getLabel()),
71+
sprintf(__('Specific format does not match: %s', 'formcreator'), $this->getTtranslatedLabel()),
7272
false,
7373
ERROR
7474
);
@@ -82,13 +82,13 @@ public function isValidValue($value): bool {
8282
$rangeMin = $parameters['range']->fields['range_min'];
8383
$rangeMax = $parameters['range']->fields['range_max'];
8484
if ($rangeMin > 0 && $value < $rangeMin) {
85-
$message = sprintf(__('The following number must be greater than %d: %s', 'formcreator'), $rangeMin, $this->getLabel());
85+
$message = sprintf(__('The following number must be greater than %d: %s', 'formcreator'), $rangeMin, $this->getTtranslatedLabel());
8686
Session::addMessageAfterRedirect($message, false, ERROR);
8787
return false;
8888
}
8989

9090
if ($rangeMax > 0 && $value > $rangeMax) {
91-
$message = sprintf(__('The following number must be lower than %d: %s', 'formcreator'), $rangeMax, $this->getLabel());
91+
$message = sprintf(__('The following number must be lower than %d: %s', 'formcreator'), $rangeMax, $this->getTtranslatedLabel());
9292
Session::addMessageAfterRedirect($message, false, ERROR);
9393
return false;
9494
}

inc/field/ldapselectfield.class.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ public function isValid(): bool {
111111
// If the field is required it can't be empty
112112
if ($this->isRequired() && $this->value == '0') {
113113
Session::addMessageAfterRedirect(
114-
__('A required field is empty:', 'formcreator') . ' ' . $this->getLabel(),
114+
__('A required field is empty:', 'formcreator') . ' ' . $this->getTtranslatedLabel(),
115115
false,
116116
ERROR
117117
);

inc/field/radiosfield.class.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ public function isValid(): bool {
188188
// If the field is required it can't be empty
189189
if ($this->isRequired() && $this->value == '') {
190190
Session::addMessageAfterRedirect(
191-
sprintf(__('A required field is empty: %s', 'formcreator'), $this->getLabel()),
191+
sprintf(__('A required field is empty: %s', 'formcreator'), $this->getTtranslatedLabel()),
192192
false,
193193
ERROR
194194
);

inc/field/requesttypefield.class.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ public function isValid(): bool {
157157
// If the field is required it can't be empty
158158
if ($this->isRequired() && $this->value == '0') {
159159
Session::addMessageAfterRedirect(
160-
__('A required field is empty:', 'formcreator') . ' ' . $this->getLabel(),
160+
__('A required field is empty:', 'formcreator') . ' ' . $this->getTtranslatedLabel(),
161161
false,
162162
ERROR
163163
);

inc/field/selectfield.class.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ public function isValid(): bool {
100100
// If the field is required it can't be empty
101101
if ($this->isRequired() && $this->value == '0') {
102102
Session::addMessageAfterRedirect(
103-
sprintf(__('A required field is empty: %s', 'formcreator'), $this->getLabel()),
103+
sprintf(__('A required field is empty: %s', 'formcreator'), $this->getTtranslatedLabel()),
104104
false,
105105
ERROR
106106
);

inc/field/tagfield.class.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ public function isValid(): bool {
160160
// If the field is required it can't be empty
161161
if ($this->isRequired() && $this->value == '') {
162162
Session::addMessageAfterRedirect(
163-
__('A required field is empty:', 'formcreator') . ' ' . $this->getLabel(),
163+
__('A required field is empty:', 'formcreator') . ' ' . $this->getTtranslatedLabel(),
164164
false,
165165
ERROR
166166
);

inc/field/textareafield.class.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,7 @@ public function isValid(): bool {
245245
// If the field is required it can't be empty
246246
if ($this->isRequired() && $this->value == '') {
247247
Session::addMessageAfterRedirect(
248-
__('A required field is empty:', 'formcreator') . ' ' . $this->getLabel(),
248+
__('A required field is empty:', 'formcreator') . ' ' . $this->getTtranslatedLabel(),
249249
false,
250250
ERROR
251251
);

inc/field/textfield.class.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ public function isValid(): bool {
122122
// If the field is required it can't be empty
123123
if ($this->isRequired() && $this->value == '') {
124124
Session::addMessageAfterRedirect(
125-
__('A required field is empty:', 'formcreator') . ' ' . $this->getLabel(),
125+
__('A required field is empty:', 'formcreator') . ' ' . $this->getTtranslatedLabel(),
126126
false,
127127
ERROR
128128
);

inc/field/timefield.class.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ public function isValid(): bool {
110110
// If the field is required it can't be empty
111111
if ($this->isRequired() && (strtotime($this->value) === false)) {
112112
Session::addMessageAfterRedirect(
113-
__('A required field is empty:', 'formcreator') . ' ' . $this->getLabel(),
113+
__('A required field is empty:', 'formcreator') . ' ' . $this->getTtranslatedLabel(),
114114
false,
115115
ERROR
116116
);

inc/field/urgencyfield.class.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ public function isValid(): bool {
162162
// If the field is required it can't be empty
163163
if ($this->isRequired() && $this->value == '0') {
164164
Session::addMessageAfterRedirect(
165-
__('A required field is empty:', 'formcreator') . ' ' . $this->getLabel(),
165+
__('A required field is empty:', 'formcreator') . ' ' . $this->getTtranslatedLabel(),
166166
false,
167167
ERROR
168168
);

tests/3-unit/PluginFormcreatorForm_Language.php

+35-26
Original file line numberDiff line numberDiff line change
@@ -80,35 +80,44 @@ public function testGetTypeName($nb, $expected) {
8080

8181
public function providerPrepareInputForAdd() {
8282
$formFk = \PluginFormcreatorForm::getForeignKeyField();
83-
return [
84-
[
85-
'input' => [
86-
$formFk => 42
87-
],
88-
'expected' => false,
89-
'expectedMessage' => 'The name cannot be empty!',
83+
yield [
84+
'input' => [
85+
$formFk => 42
9086
],
91-
[
92-
'input' => [
93-
'name' => 'foo',
94-
'comment' => 'bar',
95-
],
96-
'expected' => false,
97-
'expectedMessage' => 'The language must be associated to a form!',
87+
'expected' => false,
88+
'expectedMessage' => 'The name cannot be empty.',
89+
];
90+
yield [
91+
'input' => [
92+
'name' => 'fr_FR',
93+
'comment' => 'bar',
9894
],
99-
[
100-
'input' => [
101-
'name' => 'foo',
102-
'comment' => 'bar',
103-
$formFk => 42,
104-
],
105-
'expected' => [
106-
'name' => 'foo',
107-
'comment' => 'bar',
108-
$formFk => 42,
109-
],
110-
'expectedMessage' => '',
95+
'expected' => false,
96+
'expectedMessage' => 'The language must be associated to a form.',
97+
];
98+
99+
$form = $this->getForm();
100+
yield [
101+
'input' => [
102+
'name' => 'foo',
103+
'comment' => '',
104+
$formFk => $form->getID(),
105+
],
106+
'expected' => false,
107+
'expectedMessage' => 'The specified language is not available.',
108+
];
109+
yield [
110+
'input' => [
111+
'name' => 'fr_FR',
112+
'comment' => 'bar',
113+
$formFk => 42,
114+
],
115+
'expected' => [
116+
'name' => 'fr_FR',
117+
'comment' => 'bar',
118+
$formFk => 42,
111119
],
120+
'expectedMessage' => '',
112121
];
113122
}
114123

0 commit comments

Comments
 (0)