diff --git a/include/class.dynamic_forms.php b/include/class.dynamic_forms.php index ccfe4d27e38e5874c591afb91cfb245ece933998..c22e563fee5ad6f21a2d45e49374ed11f624913a 100644 --- a/include/class.dynamic_forms.php +++ b/include/class.dynamic_forms.php @@ -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(); + } } ?>