diff --git a/inc/abstractfield.class.php b/inc/abstractfield.class.php index f388c33eb..0dfb61e99 100644 --- a/inc/abstractfield.class.php +++ b/inc/abstractfield.class.php @@ -332,4 +332,15 @@ public function getTranslatableStrings(array $options = []) : array { return $strings; } + + /** + * Translates the label of the field into the current language + * + * @return string + */ + protected function getTtranslatedLabel(): string { + $form = PluginFormcreatorForm::getByItem($this->question); + $domain = PluginFormcreatorForm::getTranslationDomain($form->getID()); + return __($this->getLabel(), $domain); + } } diff --git a/inc/field/actorfield.class.php b/inc/field/actorfield.class.php index 8e4dea545..1d0ddc416 100644 --- a/inc/field/actorfield.class.php +++ b/inc/field/actorfield.class.php @@ -254,7 +254,7 @@ public function isValid(): bool { // If the field is required it can't be empty if ($this->isRequired() && count($this->value) === 0) { Session::addMessageAfterRedirect( - sprintf(__('A required field is empty: %s', 'formcreator'), $this->getLabel()), + sprintf(__('A required field is empty: %s', 'formcreator'), $this->getTtranslatedLabel()), false, ERROR ); @@ -264,7 +264,7 @@ public function isValid(): bool { // If an item has been removed by sanitization, then the data is not valid if (count($sanitized) != count($this->value)) { Session::addMessageAfterRedirect( - sprintf(__('Invalid value: %s', 'formcreator'), $this->getLabel()), + sprintf(__('Invalid value: %s', 'formcreator'), $this->getTtranslatedLabel()), false, ERROR ); @@ -287,7 +287,7 @@ public function isValidValue($value): bool { $user = new User(); if (!$user->getFromDB($item)) { Session::addMessageAfterRedirect( - sprintf(__('User not found or invalid email address: %s', 'formcreator'), $this->getLabel()), + sprintf(__('User not found or invalid email address: %s', 'formcreator'), $this->getTtranslatedLabel()), false, ERROR ); diff --git a/inc/field/checkboxesfield.class.php b/inc/field/checkboxesfield.class.php index 1a24ba980..07cdc072e 100644 --- a/inc/field/checkboxesfield.class.php +++ b/inc/field/checkboxesfield.class.php @@ -187,7 +187,7 @@ public function isValid(): bool { // If the field is required it can't be empty if ($this->isRequired() && count($value) <= 0) { Session::addMessageAfterRedirect( - sprintf(__('A required field is empty: %s', 'formcreator'), $this->getLabel()), + sprintf(__('A required field is empty: %s', 'formcreator'), $this->getTtranslatedLabel()), false, ERROR ); @@ -223,14 +223,14 @@ public function isValidValue($value): bool { $rangeMin = $parameters['range']->fields['range_min']; $rangeMax = $parameters['range']->fields['range_max']; if ($rangeMin > 0 && count($value) < $rangeMin) { - $message = sprintf(__('The following question needs at least %d answers', 'formcreator'), $rangeMin); - Session::addMessageAfterRedirect($message . ' ' . $this->getLabel(), false, ERROR); + $message = sprintf(__('The following question needs at least %d answers: %s', 'formcreator'), $rangeMin, $this->getTtranslatedLabel()); + Session::addMessageAfterRedirect($message, false, ERROR); return false; } if ($rangeMax > 0 && count($value) > $rangeMax) { - $message = sprintf(__('The following question does not accept more than %d answers', 'formcreator'), $rangeMax); - Session::addMessageAfterRedirect($message . ' ' . $this->getLabel(), false, ERROR); + $message = sprintf(__('The following question does not accept more than %d answers: %s', 'formcreator'), $rangeMax, $this->getTtranslatedLabel()); + Session::addMessageAfterRedirect($message, false, ERROR); return false; } } diff --git a/inc/field/datefield.class.php b/inc/field/datefield.class.php index 3c2c6dfd1..a7414885a 100644 --- a/inc/field/datefield.class.php +++ b/inc/field/datefield.class.php @@ -112,7 +112,7 @@ public function isValid(): bool { // If the field is required it can't be empty if ($this->isRequired() && (strtotime($this->value) == '')) { Session::addMessageAfterRedirect( - sprintf(__('A required field is empty: %s', 'formcreator'), $this->getLabel()), + sprintf(__('A required field is empty: %s', 'formcreator'), $this->getTtranslatedLabel()), false, ERROR ); diff --git a/inc/field/datetimefield.class.php b/inc/field/datetimefield.class.php index 7f3b2bb42..bdcc4beb8 100644 --- a/inc/field/datetimefield.class.php +++ b/inc/field/datetimefield.class.php @@ -115,7 +115,7 @@ public function isValid(): bool { // If the field is required it can't be empty if ($this->isRequired() && (strtotime($this->value) == '')) { Session::addMessageAfterRedirect( - sprintf(__('A required field is empty: %s', 'formcreator'), $this->getLabel()), + sprintf(__('A required field is empty: %s', 'formcreator'), $this->getTtranslatedLabel()), false, ERROR ); diff --git a/inc/field/dropdownfield.class.php b/inc/field/dropdownfield.class.php index 1d5b242a7..d4cd38ea5 100644 --- a/inc/field/dropdownfield.class.php +++ b/inc/field/dropdownfield.class.php @@ -438,7 +438,7 @@ public function isValid(): bool { $dropdown = new $itemtype(); if ($this->isRequired() && $dropdown->isNewId($this->value)) { Session::addMessageAfterRedirect( - __('A required field is empty:', 'formcreator') . ' ' . $this->getLabel(), + __('A required field is empty:', 'formcreator') . ' ' . $this->getTtranslatedLabel(), false, ERROR ); @@ -460,7 +460,7 @@ public function isValidValue($value): bool { if (!$isValid) { Session::addMessageAfterRedirect( - __('Invalid value for ', 'formcreator') . ' ' . $this->getLabel(), + __('Invalid value for ', 'formcreator') . ' ' . $this->getTtranslatedLabel(), false, ERROR ); diff --git a/inc/field/emailfield.class.php b/inc/field/emailfield.class.php index ec2ed4558..9df39e509 100644 --- a/inc/field/emailfield.class.php +++ b/inc/field/emailfield.class.php @@ -85,7 +85,7 @@ public function isValidValue($value): bool { if (!filter_var($value, FILTER_VALIDATE_EMAIL)) { Session::addMessageAfterRedirect( - sprintf(__('This is not a valid e-mail: %s', 'formcreator'), $this->getLabel()), + sprintf(__('This is not a valid e-mail: %s', 'formcreator'), $this->getTtranslatedLabel()), false, ERROR ); diff --git a/inc/field/filefield.class.php b/inc/field/filefield.class.php index 720b5598a..229ee2f3e 100644 --- a/inc/field/filefield.class.php +++ b/inc/field/filefield.class.php @@ -171,7 +171,7 @@ public function isValid(): bool { $key = '_formcreator_field_' . $this->question->getID(); if (($this->isRequired() && (!isset($this->uploads[$key]) || count($this->uploads[$key]) < 1))) { Session::addMessageAfterRedirect( - sprintf(__('A required file is missing: %s', 'formcreator'), $this->getLabel()), + sprintf(__('A required file is missing: %s', 'formcreator'), $this->getTtranslatedLabel()), false, ERROR ); diff --git a/inc/field/floatfield.class.php b/inc/field/floatfield.class.php index 12870de5a..19f58dcba 100644 --- a/inc/field/floatfield.class.php +++ b/inc/field/floatfield.class.php @@ -121,7 +121,7 @@ public function getDocumentsForTarget(): array { public function isValid(): bool { if ($this->isRequired() && $this->value == '') { Session::addMessageAfterRedirect( - sprintf(__('A required field is empty: %s', 'formcreator'), $this->getLabel()), + sprintf(__('A required field is empty: %s', 'formcreator'), $this->getTtranslatedLabel()), false, ERROR ); @@ -138,7 +138,7 @@ public function isValidValue($value): bool { if (!empty($value) && !is_numeric($value)) { Session::addMessageAfterRedirect( - sprintf(__('This is not a number: %s', 'formcreator'), $this->getLabel()), + sprintf(__('This is not a number: %s', 'formcreator'), $this->getTtranslatedLabel()), false, ERROR ); diff --git a/inc/field/integerfield.class.php b/inc/field/integerfield.class.php index 886d31905..21825ed13 100644 --- a/inc/field/integerfield.class.php +++ b/inc/field/integerfield.class.php @@ -56,7 +56,7 @@ public function isValidValue($value): bool { } if (!empty($value) && !ctype_digit((string) $value)) { - Session::addMessageAfterRedirect(sprintf(__('This is not an integer: %s', 'formcreator'), $this->getLabel()), false, ERROR); + Session::addMessageAfterRedirect(sprintf(__('This is not an integer: %s', 'formcreator'), $this->getTtranslatedLabel()), false, ERROR); return false; } @@ -68,7 +68,7 @@ public function isValidValue($value): bool { if ($regex !== null && strlen($regex) > 0) { if (!preg_match($regex, $value)) { Session::addMessageAfterRedirect( - sprintf(__('Specific format does not match: %s', 'formcreator'), $this->getLabel()), + sprintf(__('Specific format does not match: %s', 'formcreator'), $this->getTtranslatedLabel()), false, ERROR ); @@ -82,13 +82,13 @@ public function isValidValue($value): bool { $rangeMin = $parameters['range']->fields['range_min']; $rangeMax = $parameters['range']->fields['range_max']; if ($rangeMin > 0 && $value < $rangeMin) { - $message = sprintf(__('The following number must be greater than %d: %s', 'formcreator'), $rangeMin, $this->getLabel()); + $message = sprintf(__('The following number must be greater than %d: %s', 'formcreator'), $rangeMin, $this->getTtranslatedLabel()); Session::addMessageAfterRedirect($message, false, ERROR); return false; } if ($rangeMax > 0 && $value > $rangeMax) { - $message = sprintf(__('The following number must be lower than %d: %s', 'formcreator'), $rangeMax, $this->getLabel()); + $message = sprintf(__('The following number must be lower than %d: %s', 'formcreator'), $rangeMax, $this->getTtranslatedLabel()); Session::addMessageAfterRedirect($message, false, ERROR); return false; } diff --git a/inc/field/ldapselectfield.class.php b/inc/field/ldapselectfield.class.php index 8d1d039b8..27881f0f8 100644 --- a/inc/field/ldapselectfield.class.php +++ b/inc/field/ldapselectfield.class.php @@ -111,7 +111,7 @@ public function isValid(): bool { // If the field is required it can't be empty if ($this->isRequired() && $this->value == '0') { Session::addMessageAfterRedirect( - __('A required field is empty:', 'formcreator') . ' ' . $this->getLabel(), + __('A required field is empty:', 'formcreator') . ' ' . $this->getTtranslatedLabel(), false, ERROR ); diff --git a/inc/field/radiosfield.class.php b/inc/field/radiosfield.class.php index 87b9c7f21..7d5f331dc 100644 --- a/inc/field/radiosfield.class.php +++ b/inc/field/radiosfield.class.php @@ -188,7 +188,7 @@ public function isValid(): bool { // If the field is required it can't be empty if ($this->isRequired() && $this->value == '') { Session::addMessageAfterRedirect( - sprintf(__('A required field is empty: %s', 'formcreator'), $this->getLabel()), + sprintf(__('A required field is empty: %s', 'formcreator'), $this->getTtranslatedLabel()), false, ERROR ); diff --git a/inc/field/requesttypefield.class.php b/inc/field/requesttypefield.class.php index aea0c517e..aa9f25f9e 100644 --- a/inc/field/requesttypefield.class.php +++ b/inc/field/requesttypefield.class.php @@ -157,7 +157,7 @@ public function isValid(): bool { // If the field is required it can't be empty if ($this->isRequired() && $this->value == '0') { Session::addMessageAfterRedirect( - __('A required field is empty:', 'formcreator') . ' ' . $this->getLabel(), + __('A required field is empty:', 'formcreator') . ' ' . $this->getTtranslatedLabel(), false, ERROR ); diff --git a/inc/field/selectfield.class.php b/inc/field/selectfield.class.php index 78e0ee416..27c257cf9 100644 --- a/inc/field/selectfield.class.php +++ b/inc/field/selectfield.class.php @@ -100,7 +100,7 @@ public function isValid(): bool { // If the field is required it can't be empty if ($this->isRequired() && $this->value == '0') { Session::addMessageAfterRedirect( - sprintf(__('A required field is empty: %s', 'formcreator'), $this->getLabel()), + sprintf(__('A required field is empty: %s', 'formcreator'), $this->getTtranslatedLabel()), false, ERROR ); diff --git a/inc/field/tagfield.class.php b/inc/field/tagfield.class.php index ee97d851c..02fbda8dc 100644 --- a/inc/field/tagfield.class.php +++ b/inc/field/tagfield.class.php @@ -160,7 +160,7 @@ public function isValid(): bool { // If the field is required it can't be empty if ($this->isRequired() && $this->value == '') { Session::addMessageAfterRedirect( - __('A required field is empty:', 'formcreator') . ' ' . $this->getLabel(), + __('A required field is empty:', 'formcreator') . ' ' . $this->getTtranslatedLabel(), false, ERROR ); diff --git a/inc/field/textareafield.class.php b/inc/field/textareafield.class.php index 49de4fa6f..316b15d1b 100644 --- a/inc/field/textareafield.class.php +++ b/inc/field/textareafield.class.php @@ -245,7 +245,7 @@ public function isValid(): bool { // If the field is required it can't be empty if ($this->isRequired() && $this->value == '') { Session::addMessageAfterRedirect( - __('A required field is empty:', 'formcreator') . ' ' . $this->getLabel(), + __('A required field is empty:', 'formcreator') . ' ' . $this->getTtranslatedLabel(), false, ERROR ); diff --git a/inc/field/textfield.class.php b/inc/field/textfield.class.php index aa28d6b38..0b31a567f 100644 --- a/inc/field/textfield.class.php +++ b/inc/field/textfield.class.php @@ -122,7 +122,7 @@ public function isValid(): bool { // If the field is required it can't be empty if ($this->isRequired() && $this->value == '') { Session::addMessageAfterRedirect( - __('A required field is empty:', 'formcreator') . ' ' . $this->getLabel(), + __('A required field is empty:', 'formcreator') . ' ' . $this->getTtranslatedLabel(), false, ERROR ); diff --git a/inc/field/timefield.class.php b/inc/field/timefield.class.php index d77fde373..9f91f94f4 100644 --- a/inc/field/timefield.class.php +++ b/inc/field/timefield.class.php @@ -110,7 +110,7 @@ public function isValid(): bool { // If the field is required it can't be empty if ($this->isRequired() && (strtotime($this->value) === false)) { Session::addMessageAfterRedirect( - __('A required field is empty:', 'formcreator') . ' ' . $this->getLabel(), + __('A required field is empty:', 'formcreator') . ' ' . $this->getTtranslatedLabel(), false, ERROR ); diff --git a/inc/field/urgencyfield.class.php b/inc/field/urgencyfield.class.php index fcd9b8916..e2377475f 100644 --- a/inc/field/urgencyfield.class.php +++ b/inc/field/urgencyfield.class.php @@ -162,7 +162,7 @@ public function isValid(): bool { // If the field is required it can't be empty if ($this->isRequired() && $this->value == '0') { Session::addMessageAfterRedirect( - __('A required field is empty:', 'formcreator') . ' ' . $this->getLabel(), + __('A required field is empty:', 'formcreator') . ' ' . $this->getTtranslatedLabel(), false, ERROR ); diff --git a/tests/3-unit/PluginFormcreatorForm_Language.php b/tests/3-unit/PluginFormcreatorForm_Language.php index 29660af52..9aefbbda1 100644 --- a/tests/3-unit/PluginFormcreatorForm_Language.php +++ b/tests/3-unit/PluginFormcreatorForm_Language.php @@ -80,35 +80,44 @@ public function testGetTypeName($nb, $expected) { public function providerPrepareInputForAdd() { $formFk = \PluginFormcreatorForm::getForeignKeyField(); - return [ - [ - 'input' => [ - $formFk => 42 - ], - 'expected' => false, - 'expectedMessage' => 'The name cannot be empty!', + yield [ + 'input' => [ + $formFk => 42 ], - [ - 'input' => [ - 'name' => 'foo', - 'comment' => 'bar', - ], - 'expected' => false, - 'expectedMessage' => 'The language must be associated to a form!', + 'expected' => false, + 'expectedMessage' => 'The name cannot be empty.', + ]; + yield [ + 'input' => [ + 'name' => 'fr_FR', + 'comment' => 'bar', ], - [ - 'input' => [ - 'name' => 'foo', - 'comment' => 'bar', - $formFk => 42, - ], - 'expected' => [ - 'name' => 'foo', - 'comment' => 'bar', - $formFk => 42, - ], - 'expectedMessage' => '', + 'expected' => false, + 'expectedMessage' => 'The language must be associated to a form.', + ]; + + $form = $this->getForm(); + yield [ + 'input' => [ + 'name' => 'foo', + 'comment' => '', + $formFk => $form->getID(), + ], + 'expected' => false, + 'expectedMessage' => 'The specified language is not available.', + ]; + yield [ + 'input' => [ + 'name' => 'fr_FR', + 'comment' => 'bar', + $formFk => 42, + ], + 'expected' => [ + 'name' => 'fr_FR', + 'comment' => 'bar', + $formFk => 42, ], + 'expectedMessage' => '', ]; }