jenkins-bot has submitted this change and it was merged.

Change subject: Use helper callback to render Mustache errors
......................................................................


Use helper callback to render Mustache errors

Uses helper function fieldError to render error messages from
error_message.html.mustache with approprate css class.

Bug: T107363
Change-Id: Icde192c65d1b18ec4f4a6c8d59c4c263449efc61
---
M gateway_forms/Mustache.php
M gateway_forms/mustache/error_message.html.mustache
M gateway_forms/mustache/index.html.mustache
M gateway_forms/mustache/payment_amount.html.mustache
M gateway_forms/mustache/personal_info.html.mustache
5 files changed, 95 insertions(+), 56 deletions(-)

Approvals:
  Awight: Looks good to me, approved
  jenkins-bot: Verified



diff --git a/gateway_forms/Mustache.php b/gateway_forms/Mustache.php
index de81aca..c88cfd3 100644
--- a/gateway_forms/Mustache.php
+++ b/gateway_forms/Mustache.php
@@ -10,8 +10,18 @@
         */
        protected $topLevelForm;
 
-       // hack for l10n helper - it needs to be a static function
-       static $country;
+       const EXTENSION = '.html.mustache';
+
+       /**
+        * We set the following public static variables for use in mustache 
helper
+        * functions l10n and fieldError, which need to be static and are 
interpreted
+        * without class scope under PHP 5.3's closure rules.
+        */
+       public static $country;
+
+       public static $fieldErrors;
+
+       public static $baseDir;
 
        /**
         * @param GatewayAdapter $gateway The live adapter object that is used 
as
@@ -23,6 +33,7 @@
                // TODO: Don't hardcode like this.
                global $wgDonationInterfaceTemplate;
                $this->topLevelForm = $wgDonationInterfaceTemplate;
+               self::$baseDir = dirname( $this->topLevelForm );
        }
 
        /**
@@ -37,22 +48,41 @@
                $data = $data + $this->getUrls();
 
                self::$country = $data['country'];
+               self::$fieldErrors = $data['errors']['field'];
 
-               $template = file_get_contents( $this->topLevelForm );
+               $options = array(
+                       'helpers' => array(
+                               'l10n' => 'Gateway_Form_Mustache::l10n',
+                               'fieldError' => 
'Gateway_Form_Mustache::fieldError',
+                       ),
+                       'basedir' => array( self::$baseDir ),
+                       'fileext' => self::EXTENSION,
+               );
+               return self::render( $this->topLevelForm, $data, $options );
+       }
+
+       /**
+        * Do the rendering. Can be made protected when we're off PHP 5.3.
+        *
+        * @param string $fileName full path to template file
+        * @param array $data rendering context
+        * @param array $options options for LightnCandy::compile function
+        * @return string rendered template
+        */
+       public static function render( $fileName, $data, $options = array() ) {
+               $defaultOptions = array(
+                       'flags' => LightnCandy::FLAG_ERROR_EXCEPTION,
+               );
+
+               $options = $options + $defaultOptions;
+
+               $template = file_get_contents( $fileName );
                if ( $template === false ) {
-                       throw new RuntimeException( "Template file unavailable: 
[{$this->topLevelForm}]" );
+                       throw new RuntimeException( "Template file unavailable: 
[$fileName]" );
                }
 
-               // TODO: Use MW-core implementation, once we're on REL1_25.
-               $code = LightnCandy::compile(
-                       $template,
-                       array(
-                               'flags' => LightnCandy::FLAG_ERROR_EXCEPTION,
-                               'helpers' => array( 'l10n' => 
'Gateway_Form_Mustache::l10n' ),
-                               'basedir' => array( dirname( 
$this->topLevelForm ) ),
-                               'fileext' => '.html.mustache',
-                       )
-               );
+               // TODO: Use MW-core implementation once it allows helper 
functions
+               $code = LightnCandy::compile( $template, $options );
                if ( !$code ) {
                        throw new RuntimeException( 'Couldn\'t compile 
template!' );
                }
@@ -168,15 +198,15 @@
 
        /**
         * Get errors, sorted into two buckets - 'general' errors to display at
-        * the top of the form, and field errors to display inline.
+        * the top of the form, and 'field' errors to display inline.
         * Also get some error-related flags.
-        * @return array with all general errors under the 'general' key, and
-        *               field errors each keyed with their own field.
+        * @return array
         */
        protected function getErrors() {
                $errors = $this->gateway->getAllErrors();
                $return = array( 'errors' => array(
                        'general' => array(),
+                       'field' => array(),
                ) );
                $fieldNames = DonationData::getFieldNames();
                foreach( $errors as $key => $error ) {
@@ -191,7 +221,7 @@
                                'message' => $message,
                        );
                        if ( in_array( $key, $fieldNames ) ) {
-                               $return['errors'][$key] = $errorContext;
+                               $return['errors']['field'][$key] = 
$errorContext;
                        } else {
                                $return['errors']['general'][] = $errorContext;
                        }
@@ -212,8 +242,12 @@
                );
        }
 
+       // For the following helper functions, we can't use self:: to refer to
+       // static variables (under PHP 5.3), so we use Gateway_Form_Mustache::
+
        /**
-        * Get a message value specific to the donor's country and language
+        * Get a message value specific to the donor's country and language.
+        *
         * @param array $params first value is used as message key
         * TODO: use the rest as message parameters
         * @return string
@@ -232,6 +266,35 @@
                );
        }
 
+       /**
+        * Render a validation error message or blank error placeholder.
+        *
+        * @param array $params first should be the field name
+        * @return string
+        */
+       public static function fieldError( $params ) {
+               if ( !$params ) {
+                       throw new BadMethodCallException( 'Need field key' );
+               }
+
+               $fieldName = array_shift( $params );
+
+               if ( isset( Gateway_Form_Mustache::$fieldErrors[$fieldName] ) ) 
{
+                       $context = 
Gateway_Form_Mustache::$fieldErrors[$fieldName];
+                       $context['cssClass'] = 'errorMsg';
+               } else {
+                       $context = array(
+                               'cssClass' => 'errorMsgHide',
+                               'key' => $fieldName,
+                       );
+               }
+
+               $path = Gateway_Form_Mustache::$baseDir . DIRECTORY_SEPARATOR
+                       . 'error_message' . Gateway_Form_Mustache::EXTENSION;
+
+               return Gateway_Form_Mustache::render( $path, $context );
+       }
+
        public function getResources() {
                return array(
                        'ext.donationinterface.mustache.styles',
diff --git a/gateway_forms/mustache/error_message.html.mustache 
b/gateway_forms/mustache/error_message.html.mustache
index 2ce91d8..5eb22e8 100644
--- a/gateway_forms/mustache/error_message.html.mustache
+++ b/gateway_forms/mustache/error_message.html.mustache
@@ -1 +1,5 @@
-<span id="{{ key }}Msg" class="errorMsg" >{{{ message }}}</span>
+<tr>
+    <td>
+        <span id="{{ key }}Msg" class="{{ cssClass }}" >{{{ message }}}</span>
+    </td>
+</tr>
diff --git a/gateway_forms/mustache/index.html.mustache 
b/gateway_forms/mustache/index.html.mustache
index 3ff715d..467fb44 100644
--- a/gateway_forms/mustache/index.html.mustache
+++ b/gateway_forms/mustache/index.html.mustache
@@ -14,6 +14,7 @@
                                <div id="payment_gateway-personal-info">
                                        <table id="payment-table-donor">
                                                <tbody>
+                                               {{{ fieldError "amount" }}}
                                                <tr>
                                                        <td>
                                                                <div 
id="step1header">
diff --git a/gateway_forms/mustache/payment_amount.html.mustache 
b/gateway_forms/mustache/payment_amount.html.mustache
index e1db96c..f6ccfc1 100644
--- a/gateway_forms/mustache/payment_amount.html.mustache
+++ b/gateway_forms/mustache/payment_amount.html.mustache
@@ -14,4 +14,3 @@
                {{ amount }} {{ currency_code }}
        </span>
 </h3>
-{{# errors/amount }}{{> error_message }}{{/ errors/amount }}{{^ errors/amount 
}}<span id="amountMsg" class="errorMsgHide"></span>{{/ errors/amount }}
diff --git a/gateway_forms/mustache/personal_info.html.mustache 
b/gateway_forms/mustache/personal_info.html.mustache
index 3ce9a89..006dbb7 100644
--- a/gateway_forms/mustache/personal_info.html.mustache
+++ b/gateway_forms/mustache/personal_info.html.mustache
@@ -4,16 +4,8 @@
                                                                <input 
class="halfwidth" name="fname" value="{{ fname }}" type="text" placeholder="{{ 
l10n "donate_interface-donor-fname" }}" id="fname" required><input 
class="halfwidth" name="lname" value="{{ lname }}" type="text" placeholder="{{ 
l10n "donate_interface-donor-lname" }}" id="lname" required>
                                                        </td>
                                                </tr>
-                        <tr>
-                            <td>
-                                {{# errors/fname }}{{> error_message }}{{/ 
errors/fname }}{{^ errors/fname }}<span id="fnameMsg" 
class="errorMsgHide"></span>{{/ errors/fname }}
-                            </td>
-                        </tr>
-                        <tr>
-                            <td>
-                                {{# errors/lname }}{{> error_message }}{{/ 
errors/lname }}{{^ errors/lname }}<span id="lnameMsg" 
class="errorMsgHide"></span>{{/ errors/lname }}
-                            </td>
-                        </tr>
+                        {{{ fieldError "fname" }}}
+                        {{{ fieldError "lname" }}}
 {{/ fname_required }}
 {{# address_required }}
                                                <tr>
@@ -40,25 +32,13 @@
                                                </tr>
 
     {{# city_required }}
-                        <tr>
-                            <td>
-                                {{# errors/city }}{{> error_message }}{{/ 
errors/city }}{{^ errors/city }}<span id="cityMsg" 
class="errorMsgHide"></span>{{/ errors/city }}
-                            </td>
-                        </tr>
+                        {{{ fieldError "city" }}}
     {{/ city_required }}
     {{# state_required }}
-                        <tr>
-                            <td>
-                                {{# errors/state }}{{> error_message }}{{/ 
errors/state }}{{^ errors/state }}<span id="stateMsg" 
class="errorMsgHide"></span>{{/ errors/state }}
-                            </td>
-                        </tr>
+                        {{{ fieldError "state" }}}
     {{/ state_required }}
     {{# postal_code_required }}
-                        <tr>
-                            <td>
-                                {{# errors/postal_code }}{{> error_message 
}}{{/ errors/postal_code }}{{^ errors/postal_code }}<span id="postal_codeMsg" 
class="errorMsgHide"></span>{{/ errors/postal_code }}
-                            </td>
-                        </tr>
+                        {{{ fieldError "postal_code" }}}
     {{/ postal_code_required }}
 {{/ address_required }}
 {{# fiscal_number_required }}
@@ -67,11 +47,7 @@
                                                                <input 
class="fullwidth" name="fiscal_number" value="{{ fiscal_number }}" type="text" 
title="{{ l10n "donate_interface-donor-fiscal_number" }}" placeholder="{{ l10n 
"donate_interface-donor-fiscal_number" }}" id="fiscal_number" required >
                                                        </td>
                                                </tr>
-                        <tr>
-                            <td>
-                                {{# errors/fiscal_number }}{{> error_message 
}}{{/ errors/fiscal_number }}{{^ errors/fiscal_number }}<span 
id="fiscal_numberMsg" class="errorMsgHide"></span>{{/ errors/fiscal_number }}
-                            </td>
-                        </tr>
+                        {{{ fieldError "fiscal_number" }}}
 {{/ fiscal_number_required }}
 {{# email_required }}
                                                <tr>
@@ -79,9 +55,5 @@
                                                                <input 
class="fullwidth" name="email" value="{{ email }}" type="email" title="{{ l10n 
"donate_interface-donor-email" }}" placeholder="{{ l10n 
"donate_interface-donor-email" }}" id="email" required>
                                                        </td>
                                                </tr>
-                        <tr>
-                            <td>
-                                {{# errors/email }}{{> error_message }}{{/ 
errors/email }}{{^ errors/email }}<span id="emailMsg" 
class="errorMsgHide"></span>{{/ errors/email }}
-                            </td>
-                        </tr>
+                        {{{ fieldError "email" }}}
 {{/ email_required }}

-- 
To view, visit https://gerrit.wikimedia.org/r/284911
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: merged
Gerrit-Change-Id: Icde192c65d1b18ec4f4a6c8d59c4c263449efc61
Gerrit-PatchSet: 4
Gerrit-Project: mediawiki/extensions/DonationInterface
Gerrit-Branch: master
Gerrit-Owner: Ejegg <eeggles...@wikimedia.org>
Gerrit-Reviewer: AndyRussG <andrew.green...@gmail.com>
Gerrit-Reviewer: Awight <awi...@wikimedia.org>
Gerrit-Reviewer: Cdentinger <cdentin...@wikimedia.org>
Gerrit-Reviewer: Ejegg <eeggles...@wikimedia.org>
Gerrit-Reviewer: Ssmith <ssm...@wikimedia.org>
Gerrit-Reviewer: XenoRyet <dkozlow...@wikimedia.org>
Gerrit-Reviewer: jenkins-bot <>

_______________________________________________
MediaWiki-commits mailing list
MediaWiki-commits@lists.wikimedia.org
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to