Jump to:

17452 Posts in 4473 Topics by 1971 members


SilverStripe Forums » Archive » AutocompleteTextField

Our old forums are still available as a read-only archive.

Moderators: martimiz, Sean, biapar, Willr, Ingo, simon_w

Page: 1
Go to End
Author Topic: 2485 Views
  • julian
    Community Member
    17 Posts

    AutocompleteTextField Link to this post

    Hi all -- anyone have any idea what the optionsURL protocol is? Examples?


  • Hayden
    Core Development Team
    19 Posts

    Re: AutocompleteTextField Link to this post

    The $optionsURL is a relative URL that you use to populate a list of available options when you enter text into the field. The URL should return an unordered list of options. The text inside each list item is used to populate the field when it is selected.

    The Ajax.Autocompleter class in the prototype library will make a get request and pass the name of the field as an argument.

    For example:

    Say we have an AutocompleteTextField:

    $field = new AutocompleteTextField('AutoField', 'My autocomplete field label', 'controller/autocompleteaction');

    When text is entered into the field, it will make an ajax request behind the scenes to 'controller/autocompleteaction'.

    On your controller for the URL you have given, define the function 'autocompleteaction'. This will be called and the name of the field will be available in the $_REQUEST array:

    function autocompleteaction() {
    // find possible matches for the field
    $SQL_prefix = Convert::raw2sql($_REQUEST['AutoField']);
    $matches = DataObject::get('MyClass', "`MyClass`.`FieldToSearch` LIKE '{$SQL_prefix}%'");

    $output = '<ul>';

    if($matches) foreach($matches as $match)
    $output .= '<li>' . $match->UseToPopulateField . '</li>';

    $output .= '</ul>';

    return $output;

Page: 1
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.