Skip to content
Snippets Groups Projects
Commit f662b684 authored by Peter Rotich's avatar Peter Rotich
Browse files

Merge pull request #1343 from greezybacon/feature/selection-default


forms: Add default selection to SelectionField

Reviewed-By: default avatarPeter Rotich <peter@osticket.com>
parents 5e6785c1 81eccf03
No related branches found
No related tags found
No related merge requests found
......@@ -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(
......
......@@ -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));
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment