diff --git a/include/class.dynamic_forms.php b/include/class.dynamic_forms.php index 5a5f0baeb48e85a641b13f4864b8c11e4cff4c18..46c5045c3c867bae66eca202686517d610d610bc 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 490ad8df539b2d80fac9ec89a5a57fc01af5824c..f51a06fdc40950adda8eb7a63ed04f329ccf4c20 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(); }