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

Merge pull request #422 from greezybacon/issue/numeric-typeahead-values


forms: Fix ambiguous id / value for typeahead

Reviewed-By: default avatarPeter Rotich <peter@osticket.com>
parents 35f6feb9 27d132f4
No related branches found
No related tags found
No related merge requests found
......@@ -878,13 +878,20 @@ class SelectionField extends FormField {
}
function parse($value) {
return $this->to_php($value);
$config = $this->getConfiguration();
if (is_int($value))
return $this->to_php($this->getWidget()->getEnteredValue(), (int) $value);
elseif (!$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) {
if (!$item || ($value !== null && $value != $item->get('value'))) {
$item = DynamicListItem::lookup(array(
'value'=>$value,
'list_id'=>$this->getListId()));
......@@ -903,9 +910,13 @@ class SelectionField extends FormField {
}
function validateEntry($item) {
$config = $this->getConfiguration();
parent::validateEntry($item);
if ($item && !$item instanceof DynamicListItem)
$this->_errors[] = 'Select a value from the list';
elseif ($item && $config['typeahead']
&& $this->getWidget()->getEnteredValue() != $item->get('value'))
$this->_errors[] = 'Select a value from the list';
}
function getConfigurationOptions() {
......@@ -939,10 +950,8 @@ class SelectionWidget extends ChoicesWidget {
} elseif ($this->value) {
// Loaded from POST
$value = $this->value;
$name = DynamicListItem::lookup($value);
$name = ($name) ? $name->get('value') : $value;
$name = $this->getEnteredValue();
}
if (!$config['typeahead']) {
$this->value = $value;
return parent::render();
......@@ -951,7 +960,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 +968,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 +977,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 +985,18 @@ 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();
}
function getEnteredValue() {
// Used to verify typeahead fields
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