From 92a04f391dc9915e21ae07ac24b62d7556362fff Mon Sep 17 00:00:00 2001 From: Jared Hancock <jared@osticket.com> Date: Wed, 27 Aug 2014 09:58:10 -0500 Subject: [PATCH] forms: Fix typeahead field again (for realz this time) --- include/class.dynamic_forms.php | 37 +++++++++++++++++++++------------ include/class.forms.php | 4 ++-- 2 files changed, 26 insertions(+), 15 deletions(-) diff --git a/include/class.dynamic_forms.php b/include/class.dynamic_forms.php index 5a5f0baeb..46c5045c3 100644 --- a/include/class.dynamic_forms.php +++ b/include/class.dynamic_forms.php @@ -941,7 +941,7 @@ class DynamicFormEntryAnswer extends VerySimpleModel { } class SelectionField extends FormField { - static $widget = 'SelectionWidget'; + static $widget = 'ChoicesWidget'; function getListId() { list(,$list_id) = explode('-', $this->get('type')); @@ -955,6 +955,14 @@ class SelectionField extends FormField { return $this->_list; } + function getWidget() { + $config = $this->getConfiguration(); + $widgetClass = false; + if ($config['widget'] == 'typeahead') + $widgetClass = 'TypeaheadSelectionWidget'; + return parent::getWidget($widgetClass); + } + function parse($value) { if (!($list=$this->getList())) @@ -1108,19 +1116,15 @@ class SelectionField extends FormField { } } -class SelectionWidget extends ChoicesWidget { - function render($mode=false) { - - $config = $this->field->getConfiguration(); - $value = $this->value; - - if (!$config['typeahead'] || $mode=='search') { - return parent::render($mode); - } +class TypeaheadSelectionWidget extends ChoicesWidget { + function render($how) { + if ($how == 'search') + return parent::render($how); - if ($value && is_array($value)) { - $name = $this->getEnteredValue() ?: current($value); - $value = key($value); + $name = $this->getEnteredValue(); + if (is_array($this->value)) { + $name = $name ?: current($this->value); + $value = key($this->value); } $source = array(); @@ -1158,6 +1162,13 @@ class SelectionWidget extends ChoicesWidget { <?php } + function getValue() { + $data = $this->field->getSource(); + if (isset($data[$this->name])) + return $data[$this->name]; + return parent::getValue(); + } + function getEnteredValue() { // Used to verify typeahead fields $data = $this->field->getSource(); diff --git a/include/class.forms.php b/include/class.forms.php index 490ad8df5..f51a06fdc 100644 --- a/include/class.forms.php +++ b/include/class.forms.php @@ -506,11 +506,11 @@ class FormField { $this->_config[$prop] = $value; } - function getWidget() { + function getWidget($widgetClass=false) { if (!static::$widget) throw new Exception(__('Widget not defined for this field')); if (!isset($this->_widget)) { - $wc = $this->get('widget') ? $this->get('widget') : static::$widget; + $wc = $widgetClass ?: $this->get('widget') ?: static::$widget; $this->_widget = new $wc($this); $this->_widget->parseValue(); } -- GitLab