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