Newer
Older
return array($item->value, $item->id);
return null;
}
function toString($item) {
return ($item instanceof DynamicListItem)
? $item->toString() : (string) $item;
}
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() {
return array(
'typeahead' => new ChoiceField(array(
'id'=>1, 'label'=>'Widget', 'required'=>false,
'default'=>false,
'choices'=>array(false=>'Drop Down', true=>'Typeahead'),
'hint'=>'Typeahead will work better for large lists'
)),
'prompt' => new TextboxField(array(
'id'=>2, 'label'=>'Prompt', 'required'=>false, 'default'=>'',
'hint'=>'Leading text shown before a value is selected',
'configuration'=>array('size'=>40, 'length'=>40),
)),
function getChoices() {
if (!$this->_choices) {
$this->_choices = array();
foreach ($this->getList()->getItems() as $i)
$this->_choices[$i->get('id')] = $i->get('value');
}
return $this->_choices;
}
function export($value) {
if ($value && is_numeric($value)
&& ($item = DynamicListItem::lookup($value)))
return $item->toString();
return $value;
}
}
class SelectionWidget extends ChoicesWidget {
$config = $this->field->getConfiguration();
$value = false;
if ($this->value instanceof DynamicListItem) {
// Loaded from database
$value = $this->value->get('id');
$name = $this->value->get('value');
} elseif ($this->value) {
// Loaded from POST
$value = $this->value;
$name = $this->getEnteredValue();
if (!$config['typeahead'] || $mode=='search') {
}
$source = array();
foreach ($this->field->getList()->getItems() as $i)
$source[] = array(
'value' => $i->get('value'), 'id' => $i->get('id'),
'info' => $i->get('value')." -- ".$i->get('extra'),
);
<input type="text" size="30" name="<?php echo $this->name; ?>"
id="<?php echo $this->name; ?>" value="<?php echo $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; ?>"/>
<script type="text/javascript">
$(function() {
$('input#<?php echo $this->name; ?>').typeahead({
source: <?php echo JsonDataEncoder::encode($source); ?>,
$('input#<?php echo $this->name; ?>').val(item['value'])
$('input#<?php echo $this->name; ?>_id').val(item['id'])
}
});
});
</script>
</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();
}