From 617a8254a8d80c7af9eb0cf46b1928e2f1c61ae4 Mon Sep 17 00:00:00 2001
From: Demian Katz <demian.katz@villanova.edu>
Date: Mon, 13 Jan 2020 09:00:04 -0500
Subject: [PATCH] Initialize feedback messages earlier. (#1534)

- Prevents a problem where the messages are missing if you skip the setFormId() call.
- This was causing test failures in PHP 7.4.
---
 module/VuFind/src/VuFind/Form/Form.php | 32 +++++++++++++++++---------
 1 file changed, 21 insertions(+), 11 deletions(-)

diff --git a/module/VuFind/src/VuFind/Form/Form.php b/module/VuFind/src/VuFind/Form/Form.php
index 327726d5c9a..867b7d83fa4 100644
--- a/module/VuFind/src/VuFind/Form/Form.php
+++ b/module/VuFind/src/VuFind/Form/Form.php
@@ -54,11 +54,14 @@ class Form extends \Zend\Form\Form implements
     protected $inputFilter;
 
     /**
-     * Validation messages
+     * Default, untranslated validation messages
      *
      * @var array
      */
-    protected $messages;
+    protected $messages = [
+        'empty' => 'This field is required',
+        'invalid_email' => 'Email address is invalid',
+    ];
 
     /**
      * Default form config (from config.ini > Feedback)
@@ -118,13 +121,6 @@ class Form extends \Zend\Form\Form implements
             throw new \VuFind\Exception\RecordMissing("Form '$formId' not found");
         }
 
-        $this->messages = [];
-        $this->messages['empty']
-            = $this->translate('This field is required');
-
-        $this->messages['invalid_email']
-            = $this->translate('Email address is invalid');
-
         $this->formElementConfig
             = $this->parseConfig($formId, $config);
 
@@ -636,6 +632,20 @@ class Form extends \Zend\Form\Form implements
         return [$params, 'Email/form.phtml'];
     }
 
+    /**
+     * Get translated validation message.
+     *
+     * @param string $messageId Message identifier
+     *
+     * @return string
+     */
+    protected function getValidationMessage($messageId)
+    {
+        return $this->translate(
+            $this->messages[$messageId] ?? $messageId
+        );
+    }
+
     /**
      * Retrieve input filter used by this form
      *
@@ -653,14 +663,14 @@ class Form extends \Zend\Form\Form implements
             'email' => [
                 'name' => EmailAddress::class,
                 'options' => [
-                    'message' => $this->messages['invalid_email']
+                    'message' => $this->getValidationMessage('invalid_email'),
                 ]
             ],
             'notEmpty' => [
                 'name' => NotEmpty::class,
                 'options' => [
                     'message' => [
-                        NotEmpty::IS_EMPTY => $this->messages['empty']
+                        NotEmpty::IS_EMPTY => $this->getValidationMessage('empty'),
                     ]
                 ]
             ]
-- 
GitLab