diff --git a/include/class.dynamic_forms.php b/include/class.dynamic_forms.php index 039189a2788f365212629b66674c73b5da7a14fe..59605162dbc48e7227f89d651d4753340057cad4 100644 --- a/include/class.dynamic_forms.php +++ b/include/class.dynamic_forms.php @@ -1070,7 +1070,7 @@ class SelectionField extends FormField { function getListId() { list(,$list_id) = explode('-', $this->get('type')); - return $list_id; + return $list_id ?: $this->get('list_id'); } function getList() { @@ -1186,6 +1186,11 @@ class SelectionField extends FormField { 'hint'=>__('Leading text shown before a value is selected'), 'configuration'=>array('size'=>40, 'length'=>40), )), + 'default' => new SelectionField(array( + 'id'=>4, 'label'=>__('Default'), 'required'=>false, 'default'=>'', + 'list_id'=>$this->getListId(), + 'configuration' => array('prompt'=>__('Select a Default')), + )), ); } @@ -1256,12 +1261,20 @@ class TypeaheadSelectionWidget extends ChoicesWidget { return parent::render($how); $name = $this->getEnteredValue(); + $config = $this->field->getConfiguration(); if (is_array($this->value)) { $name = $name ?: current($this->value); $value = key($this->value); } + else { + // Pull configured default (if configured) + $def_key = $this->field->get('default'); + if (!$def_key && $config['default']) + $def_key = $config['default']; + if (is_array($def_key)) + $name = current($def_key); + } - $config = $this->field->getConfiguration(); $source = array(); foreach ($this->field->getList()->getItems() as $i) $source[] = array( diff --git a/include/class.forms.php b/include/class.forms.php index 090ecbb2686fc0f00200abecac1b380f3e23cb4c..a9dc57fb615f712a1c391d693999cf5861d2bd03 100644 --- a/include/class.forms.php +++ b/include/class.forms.php @@ -609,7 +609,7 @@ class FormField { if (!$this->_cform) { $type = static::getFieldType($this->get('type')); $clazz = $type[1]; - $T = new $clazz(); + $T = new $clazz(array('type'=>$this->get('type'))); $config = $this->getConfiguration(); $this->_cform = new Form($T->getConfigurationOptions(), $source); if (!$source) { @@ -1817,12 +1817,14 @@ class ChoicesWidget extends Widget { $def_key = $this->field->get('default'); if (!$def_key && $config['default']) $def_key = $config['default']; + if (is_array($def_key)) + $def_key = key($def_key); $have_def = isset($choices[$def_key]); $def_val = $have_def ? $choices[$def_key] : $prompt; } $values = $this->value; - if (!is_array($values)) { + if (!is_array($values) && $values) { $values = array($values => $this->field->getChoice($values)); }