Skip to content
Snippets Groups Projects
Commit ef2e481d authored by Jared Hancock's avatar Jared Hancock
Browse files

forms: Fixup the SelectionField wrt. multiple values

Always support and consider multiple values — even using the typeahead
widget.
parent 4a49ddd1
No related branches found
No related tags found
No related merge requests found
......@@ -963,16 +963,9 @@ class SelectionField extends FormField {
$config = $this->getConfiguration();
$choices = $this->getChoices();
$selection = array();
if ($config['typeahead']) {
// Entered value
$val = $this->getWidget()->getEnteredValue();
if (($i=$list->getItem($val)) && $i->getId() == $value)
$selection[$i->getId()] = $i->getValue();
elseif ($val && isset($choices[$value])) //perhaps old deleted item...
$selection[$value] = $choices[$value];
} elseif ($value && is_array($value)) {
foreach ($value as $v) {
if (($i=$list->getItem((int) $v)))
if ($value && is_array($value)) {
foreach ($value as $k=>$v) {
if (($i=$list->getItem((int) $k)))
$selection[$i->getId()] = $i->getValue();
elseif (isset($choices[$v]))
$selection[$v] = $choices[$v];
......@@ -995,8 +988,14 @@ class SelectionField extends FormField {
}
function to_php($value, $id=false) {
return ($value && !is_array($value))
$value = ($value && !is_array($value))
? JsonDataParser::parse($value) : $value;
if (!is_array($value)) {
$choices = $this->getChoices();
if (isset($choices[$value]))
$value = $choices[$value];
}
return $value;
}
function hasIdValue() {
......@@ -1082,6 +1081,18 @@ class SelectionField extends FormField {
return $this->_choices;
}
function getChoice($value) {
$choices = $this->getChoices();
if ($value && is_array($value)) {
$selection = $value;
} elseif (isset($choices[$value]))
$selection[] = $choices[$value];
elseif ($this->get('default'))
$selection[] = $choices[$this->get('default')];
return $selection;
}
function export($value) {
if ($value && is_numeric($value)
&& ($item = DynamicListItem::lookup($value)))
......@@ -1102,18 +1113,14 @@ class SelectionWidget extends ChoicesWidget {
function render($mode=false) {
$config = $this->field->getConfiguration();
if (($value=$this->getValue()))
$value = $this->field->parse($value);
elseif ($this->value)
$value = $this->value;
$value = $this->value;
if (!$config['typeahead'] || $mode=='search') {
$this->value = $value;
return parent::render($mode);
}
if ($value && is_array($value)) {
$name = current($value);
$name = $this->getEnteredValue() ?: current($value);
$value = key($value);
}
......@@ -1127,20 +1134,23 @@ class SelectionWidget extends ChoicesWidget {
);
?>
<span style="display:inline-block">
<input type="text" size="30" name="<?php echo $this->name; ?>"
id="<?php echo $this->name; ?>" value="<?php echo $name; ?>"
<input type="text" size="30" name="<?php echo $this->name; ?>_name"
id="<?php echo $this->name; ?>" value="<?php echo Format::htmlchars($name); ?>"
placeholder="<?php echo $config['prompt'];
?>" autocomplete="off" />
<input type="hidden" name="<?php echo $this->name;
?>_id" id="<?php echo $this->name; ?>_id" value="<?php echo $value; ?>"/>
?>[<?php echo $value; ?>]" id="<?php echo $this->name;
?>_id" value="<?php echo Format::htmlchars($name); ?>"/>
<script type="text/javascript">
$(function() {
$('input#<?php echo $this->name; ?>').typeahead({
source: <?php echo JsonDataEncoder::encode($source); ?>,
property: 'info',
onselect: function(item) {
$('input#<?php echo $this->name; ?>').val(item['value'])
$('input#<?php echo $this->name; ?>_id').val(item['id'])
$('input#<?php echo $this->name; ?>_name').val(item['value'])
$('input#<?php echo $this->name; ?>_id')
.attr('name', '<?php echo $this->name; ?>[' + item['id'] + ']')
.val(item['value']);
}
});
});
......@@ -1149,16 +1159,11 @@ class SelectionWidget extends ChoicesWidget {
<?php
}
function getValue() {
$data = $this->field->getSource();
// Search for HTML form name first
if (isset($data[$this->name.'_id']))
return (int) $data[$this->name.'_id'];
return parent::getValue();
}
function getEnteredValue() {
// Used to verify typeahead fields
$data = $this->field->getSource();
if (isset($data[$this->name.'_name']))
return trim($data[$this->name.'_name']);
return parent::getValue();
}
}
......
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