Hi all -- anyone have any idea what the optionsURL protocol is? Examples?
thanks
J
This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.
Please use forum.silverstripe.org for any new questions
(announcement).
The forum archive will stick around, but will be read only.
You can also use our Slack channel
or StackOverflow to ask for help.
Check out our community overview for more options to contribute.
Hi all -- anyone have any idea what the optionsURL protocol is? Examples?
thanks
J
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;
}