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

forms: Fix ambiguous id / value for typeahead

If a dynamic list is displayed as a typeahead box, the value, when saved,
will be confused with the corresponding ID field for the list. MySQL
supports numeric comparison between the INT id column and the textual value
column. Therefore, if a typeahead field has the value of '01 - GooGoo', then
record will match the ListItem<id=1>, regardless of which list the item is
defined for.

This patch resolves the issue by submitting the ID number of the list
selection in the HTTP form.
parent e9a3b985
No related branches found
No related tags found
No related merge requests found
......@@ -878,11 +878,16 @@ class SelectionField extends FormField {
}
function parse($value) {
return $this->to_php($value);
$config = $this->getConfiguration();
if (is_int($value) || !$config['typeahead'])
return $this->to_php(null, (int) $value);
else
return $this->to_php($value);
}
function to_php($value, $id=false) {
$item = DynamicListItem::lookup($id ? $id : $value);
if ($id && is_int($id))
$item = DynamicListItem::lookup($id);
# Attempt item lookup by name too
if (!$item) {
$item = DynamicListItem::lookup(array(
......@@ -951,7 +956,7 @@ class SelectionWidget extends ChoicesWidget {
$source = array();
foreach ($this->field->getList()->getItems() as $i)
$source[] = array(
'value' => $i->get('value'),
'value' => $i->get('value'), 'id' => $i->get('id'),
'info' => $i->get('value')." -- ".$i->get('extra'),
);
?>
......@@ -959,6 +964,8 @@ class SelectionWidget extends ChoicesWidget {
<input type="text" size="30" name="<?php echo $this->name; ?>"
id="<?php echo $this->name; ?>" value="<?php echo $name; ?>"
autocomplete="off" />
<input type="hidden" name="<?php echo $this->name;
?>_id" id="<?php echo $this->name; ?>_id" value="<?php echo $value; ?>"/>
<script type="text/javascript">
$(function() {
$('input#<?php echo $this->name; ?>').typeahead({
......@@ -966,6 +973,7 @@ class SelectionWidget extends ChoicesWidget {
property: 'info',
onselect: function(item) {
$('input#<?php echo $this->name; ?>').val(item['value'])
$('input#<?php echo $this->name; ?>_id').val(item['id'])
}
});
});
......@@ -973,5 +981,13 @@ class SelectionWidget extends ChoicesWidget {
</span>
<?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();
}
}
?>
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