From aa9190ff906720e9fdb5baa9c11c801c97773775 Mon Sep 17 00:00:00 2001
From: Jared Hancock <jared@osticket.com>
Date: Mon, 20 Jul 2015 16:26:57 -0500
Subject: [PATCH] selection: Fixup use of default on typeahead widget for
 selections

---
 include/class.dynamic_forms.php | 19 ++++++++++++-------
 1 file changed, 12 insertions(+), 7 deletions(-)

diff --git a/include/class.dynamic_forms.php b/include/class.dynamic_forms.php
index 8af03362e..d49186e7c 100644
--- a/include/class.dynamic_forms.php
+++ b/include/class.dynamic_forms.php
@@ -1848,8 +1848,8 @@ class TypeaheadSelectionWidget extends ChoicesWidget {
             placeholder="<?php echo $config['prompt'];
             ?>" autocomplete="off" />
         <input type="hidden" name="<?php echo $this->name;
-            ?>[<?php echo $value; ?>]" id="<?php echo $this->name;
-            ?>_id" value="<?php echo Format::htmlchars($name); ?>"/>
+            ?>_id" id="<?php echo $this->name;
+            ?>_id" value="<?php echo Format::htmlchars($value); ?>"/>
         <script type="text/javascript">
         $(function() {
             $('input#<?php echo $this->name; ?>').typeahead({
@@ -1875,12 +1875,17 @@ class TypeaheadSelectionWidget extends ChoicesWidget {
 
     function getValue() {
         $data = $this->field->getSource();
-        if (isset($data[$this->name]))
-            return $data[$this->name];
-
         $name = $this->field->get('name');
-        if (isset($data[$name]))
-           return $data[$name];
+        if (isset($data["{$this->name}_id"]) && is_numeric($data["{$this->name}_id"])) {
+            return array($data["{$this->name}_id"] => $data["{$this->name}_name"]);
+        }
+        elseif (isset($data[$name])) {
+            return $data[$name];
+        }
+        // Attempt to lookup typed value (usually from a default)
+        elseif ($val = $this->getEnteredValue()) {
+            return $this->field->lookupChoice($val);
+        }
 
         return parent::getValue();
     }
-- 
GitLab