Nate has published a nice component which makes the creation of an input field with Ajax autocompletion very easy. In fact, it is (almost) as easy as creating a simple input field. You can get the component from http://github.com/cakebaker/autocomplete-component (the original component from Nate is available on CakeForge, though it no longer works with current CakePHP versions).
What do you have to do? Put the component to your app/controllers/components directory. In your controller you have to include the component with:
var $components = array('Autocomplete');
Also make sure that you have defined the Ajax helper in your controller. I usually add it to the helpers array in my AppController (app/app_controller.php):
After that, you can add the autocompleter to your view.
<?php echo $ajax->autocomplete('Company.name'); ?>
One condition must be fullfilled to make this code snippet work: the controller which renders the view must have access to the appropriate model, in our example to the Company model.
That’s it :)
Thanks to Nate for the hint.
Update 2010-03-08: Added a paragraph about the requirement of the Prototype and Scriptaculous libraries. Thanks to Andy Potanin for the hint.