Version: current

     1 <?php
     2 /**
     3  * Autocompleting text field, using script.aculo.us
     4  * @package forms
     5  * @subpackage fields-formattedinput
     6  */
     7 class AutocompleteTextField extends TextField {
     8
     9     protected $optionsURL;
    10
    11     function __construct($name, $title = null, $optionsURL, $value = "", $maxLength = null){
    12         $this->optionsURL = $optionsURL;
    13
    14         parent::__construct($name, $title, $value, $maxLength);
    15     }
    16
    17     function extraClass() {
    18         return parent::extraClass() . " autocomplete";
    19     }
    20
    21     function Field() {
    22         // Requirements::javascript(SAPPHIRE_DIR . '/javascript/AutocompleteTextField.js');
    23         $attributes = array(
    24             'class' => "{$this->class} text" . ($this->extraClass() ? $this->extraClass() : ''),
    25             'type' => 'text',
    26             'id' => $this->id(),
    27             'name' => $this->name,
    28             'value' => $this->Value(),
    29             'tabindex' => $this->getTabIndex(),
    30             'size' => $this->maxLength ? min( $this->maxLength, 30 ) : 30
    31         );
    32         if($this->maxLength) $attributes['maxlength'] = $this->maxLength;
    33
    34         return $this->createTag('input', $attributes) . "<div id=\"" . $this->id() . "_Options\" class=\"autocompleteoptions\"></div>";
    35     }
    36
    37     function FieldHolder() {
    38         $holder = parent::FieldHolder();
    39
    40         $id = $this->id();
    41
    42         $holder .= <<<JS
    43             <script type="text/javascript">
    44                 new Ajax.Autocompleter( '$id', '{$id}_Options', '{$this->optionsURL}', { afterUpdateElement : function(el) { if(el.onajaxupdate) { el.onajaxupdate(); } } } );
    45             </script>
    46 JS;
    47
    48         return $holder;
    49     }
    50 }
    51 ?>

[Raise a SilverStripe Framework issue/bug](https://github.com/silverstripe/silverstripe-framework/issues/new)
- [Raise a SilverStripe CMS issue/bug](https://github.com/silverstripe/silverstripe-cms/issues/new)
- Please use the Silverstripe Forums to ask development related questions. -