Last active
August 29, 2015 14:07
-
-
Save McGaiser/c605780d54b62c78abbb to your computer and use it in GitHub Desktop.
New Widget issues
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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