From a126f2b6b3867a7f6fcae1bf36d4844cd329a87f Mon Sep 17 00:00:00 2001
From: Carsten  Rose <carsten.rose@math.uzh.ch>
Date: Tue, 10 May 2016 17:31:12 +0200
Subject: [PATCH] Fixed a bug that date-/time inputs are rendered with a size=0
 and/or maxlength=0. Fixed a bug that Input fields are not rendered correctly
 the columntype are 'set' or 'enum'. Support.php: doAttribute() - attributes
 with type 'size' or 'maxlenght' and no value or value=0 are skipped and not
 created. AbstractBuild.php: a maxLenght formelement parameter needs to be
 numeric and (new) >0 to have an impact. Added calculation of maxlength for
 columntypes 'set' and 'enum': maxLengthSetEnum().

---
 extension/qfq/qfq/AbstractBuildForm.php | 28 ++++++++++++++++++++++++-
 extension/qfq/qfq/helper/Support.php    | 15 ++++++++++++-
 2 files changed, 41 insertions(+), 2 deletions(-)

diff --git a/extension/qfq/qfq/AbstractBuildForm.php b/extension/qfq/qfq/AbstractBuildForm.php
index e2c71ba98..b5b12ef36 100644
--- a/extension/qfq/qfq/AbstractBuildForm.php
+++ b/extension/qfq/qfq/AbstractBuildForm.php
@@ -662,7 +662,7 @@ abstract class AbstractBuildForm {
 
         // date/datetime
         if ($maxLength !== false) {
-            if (is_numeric($formElement['maxLength'])) {
+            if (is_numeric($formElement['maxLength'] && $formElement['maxLength'] != '0')) {
                 if ($formElement['maxLength'] > $maxLength) {
                     $formElement['maxLength'] = $maxLength;
                 }
@@ -691,6 +691,9 @@ abstract class AbstractBuildForm {
             case 'time': // hh:mm:ss
                 return 8;
             default:
+                if (substr($typeSpec, 0, 4) === 'set(' || substr($typeSpec, 0, 5) === 'enum(') {
+                    return $this->maxLengthSetEnum($typeSpec);
+                }
                 break;
         }
 
@@ -703,6 +706,29 @@ abstract class AbstractBuildForm {
         return false;
     }
 
+    /**
+     * Get the strlen of the longest element in enum('val1','val2',...,'valn') or set('val1','val2',...,'valn')
+     *
+     * @param string $typeSpec
+     * @return int
+     */
+    private function maxLengthSetEnum($typeSpec) {
+        $startPos = (substr($typeSpec, 0, 4) === 'set(') ? 4 : 5;
+        $max = 0;
+
+        $valueList = substr($typeSpec, $startPos, strlen($typeSpec) - $startPos - 1);
+        $valueArr = explode(',', $valueList);
+        foreach ($valueArr as $value) {
+            $value = trim($value, "'");
+            $len = strlen($value);
+            if ($len > $max) {
+                $max = $len;
+            }
+        }
+
+        return $max;
+    }
+
     /**
      * Builds a HTML attribute list, based on  $attributeList.
      *
diff --git a/extension/qfq/qfq/helper/Support.php b/extension/qfq/qfq/helper/Support.php
index 60fd97cf8..6bf614400 100644
--- a/extension/qfq/qfq/helper/Support.php
+++ b/extension/qfq/qfq/helper/Support.php
@@ -90,8 +90,21 @@ class Support {
      * @return string
      */
     public static function doAttribute($type, $value, $flagOmitEmpty = true) {
-        if ($flagOmitEmpty && $value === "")
+        if ($flagOmitEmpty && $value === "") {
             return '';
+        }
+
+        switch ($type) {
+            case 'size':
+            case 'maxlength':
+                // empty or '0' for attributes of type 'size' or 'maxlenght' result in unsuable input elements: skip this.
+                if ($value === '' || $value == 0) {
+                    return '';
+                }
+                break;
+            default:
+                break;
+        }
 
         return $type . '="' . trim($value) . '" ';
     }
-- 
GitLab