Skip to content

Commit

Permalink
fix: translate field label in error messages
Browse files Browse the repository at this point in the history
btry committed May 15, 2023

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
1 parent 261e536 commit a4bf10a
Showing 20 changed files with 75 additions and 55 deletions.
11 changes: 11 additions & 0 deletions inc/abstractfield.class.php
Original file line number Diff line number Diff line change
@@ -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);
}
}
6 changes: 3 additions & 3 deletions inc/field/actorfield.class.php
Original file line number Diff line number Diff line change
@@ -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
);
10 changes: 5 additions & 5 deletions inc/field/checkboxesfield.class.php
Original file line number Diff line number Diff line change
@@ -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;
}
}
2 changes: 1 addition & 1 deletion inc/field/datefield.class.php
Original file line number Diff line number Diff line change
@@ -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
);
2 changes: 1 addition & 1 deletion inc/field/datetimefield.class.php
Original file line number Diff line number Diff line change
@@ -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
);
4 changes: 2 additions & 2 deletions inc/field/dropdownfield.class.php
Original file line number Diff line number Diff line change
@@ -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
);
2 changes: 1 addition & 1 deletion inc/field/emailfield.class.php
Original file line number Diff line number Diff line change
@@ -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
);
2 changes: 1 addition & 1 deletion inc/field/filefield.class.php
Original file line number Diff line number Diff line change
@@ -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
);
4 changes: 2 additions & 2 deletions inc/field/floatfield.class.php
Original file line number Diff line number Diff line change
@@ -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
);
8 changes: 4 additions & 4 deletions inc/field/integerfield.class.php
Original file line number Diff line number Diff line change
@@ -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;
}
2 changes: 1 addition & 1 deletion inc/field/ldapselectfield.class.php
Original file line number Diff line number Diff line change
@@ -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
);
2 changes: 1 addition & 1 deletion inc/field/radiosfield.class.php
Original file line number Diff line number Diff line change
@@ -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
);
2 changes: 1 addition & 1 deletion inc/field/requesttypefield.class.php
Original file line number Diff line number Diff line change
@@ -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
);
2 changes: 1 addition & 1 deletion inc/field/selectfield.class.php
Original file line number Diff line number Diff line change
@@ -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
);
2 changes: 1 addition & 1 deletion inc/field/tagfield.class.php
Original file line number Diff line number Diff line change
@@ -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
);
2 changes: 1 addition & 1 deletion inc/field/textareafield.class.php
Original file line number Diff line number Diff line change
@@ -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
);
2 changes: 1 addition & 1 deletion inc/field/textfield.class.php
Original file line number Diff line number Diff line change
@@ -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
);
2 changes: 1 addition & 1 deletion inc/field/timefield.class.php
Original file line number Diff line number Diff line change
@@ -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
);
2 changes: 1 addition & 1 deletion inc/field/urgencyfield.class.php
Original file line number Diff line number Diff line change
@@ -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
);
61 changes: 35 additions & 26 deletions tests/3-unit/PluginFormcreatorForm_Language.php
Original file line number Diff line number Diff line change
@@ -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' => '',
];
}

0 comments on commit a4bf10a

Please sign in to comment.