PHP code example of clesson-de / silverstripe-autocomplete

1. Go to this page and download the library: Download clesson-de/silverstripe-autocomplete library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

clesson-de / silverstripe-autocomplete example snippets


use Clesson\Silverstripe\Autocomplete\Forms\AutocompleteField;

// 1) Model + property source
$cityField = AutocompleteField::create('City', 'City')
    ->setSourceModel(\App\Models\Address::class, 'City')
    ->setLimit(8)
    ->setMinChars(2);

// 2) Callback source
$streetField = AutocompleteField::create('Street', 'Street')
    ->setSourceCallback([\App\Services\AddressService::class, 'searchStreetNames'])
    ->setLimit(10);

$fields->addFieldsToTab('Root.Main', [$cityField, $streetField]);

public static function searchStreetNames(string $query): array
{
    if ($query === '') {
        return [];
    }

    return [
        'Main Street',
        'Market Street',
    ];
}