Skip to content

Instantly share code, notes, and snippets.

@McGaiser
Last active August 29, 2015 14:07
Show Gist options
  • Save McGaiser/c605780d54b62c78abbb to your computer and use it in GitHub Desktop.
Save McGaiser/c605780d54b62c78abbb to your computer and use it in GitHub Desktop.
New Widget issues
//This is my autocomplete widget
<?php
namespace App\View\Widget;
use Cake\View\Widget\WidgetInterface;
use Cake\View\Form\ContextInterface;
class Autocomplete implements WidgetInterface {
protected $_templates;
public function __construct($templates) {
$this->_templates = $templates;
}
public function render(array $data, ContextInterface $context) {
$data += [
'name' => 'hi',
];
return $this->_templates->format('autocomplete', [
'name' => $data['name'],
'attrs' => $this->_templates->formatAttributes($data, ['name'])
]);
}
public function secureFields(array $data){
return [];
}
}
?>
//The helper include within the appcontroller file
public $helpers = [
'Access',
'Form' => [
'widgets' => [
'autocomplete' => [
'App\View\Widget\Autocomplete',
'text',
'label'
]
],
'templates' => 'app_form.php',
]
];
Then the search field that I want to autocomplete in my index.ctp file
<div>
<?= $this->Form->create() ?>
<fieldset>
<?php
echo $this->Form->input('search', ['type' => 'autocomplete']);
?>
</fieldset>
<?= $this->Form->button(__('Submit')) ?>
<?= $this->Form->end() ?>
</div>
//And the contents of the app_form.php
<?php
$config = [
'autocomplate' => '<div class="form-autocomplete">{{content}}</div>',
];
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment