Tuesday, September 30, 2014

Re: CakePHP 3.0 autocomplete

1. /src/View/Widget/Autocomplete.php

<?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' => '',
       
];
       
return $this->_templates->format('autocomplete', [
           
'name' => $data['name'],
           
'attrs' => $this->_templates->formatAttributes($data, ['name'])
       
]);
   
}
   
   
public function secureFields(array $data){
       
return [];
   
}
}
?>

2. /src/Controller/AppController.php

<?php
namespace App\Controller;

use Cake\Controller\Controller;
use Cake\Event\Event;

class AppController extends Controller {

   
public $components = [
       
'Flash',
       
'Auth' => [
           
'loginRedirect' => [
               
'controller' => 'users',
               
'action' => 'view'
           
],
           
'logoutRedirect' => [
               
'controller' => 'Pages',
               
'action' => 'display',
               
'home'
           
]
       
]
   
];
   
   
public $helpers = [
       
'Form' => [
           
'widgets' => [
               
'autocomplete' => [
                   
'App\View\Widget\Autocomplete',
                   
'text',
                   
'label'
               
]
           
]
       
]
   
];

   
public function beforeFilter(Event $event) {
        $this
->Auth->deny();
   
}
}



3. /src/Template/Contacts/add.ctp

<div class="actions columns large-2 medium-3">
   
<h3><?= __('Actions') ?></h3>
   
<ul class="side-nav">
       
<li><?= $this->Html->link(__('List Contacts'), ['action' => 'index']) ?></li>
   
</ul>
</div>
<div class="contacts form large-10 medium-9 columns">
<?= $this->Form->create($contact) ?>
   
<fieldset>
       
<legend><?= __('Add Contact'); ?></legend>
   
<?php
        echo $this
->Form->autocomplete('name');
   
?>
   
</fieldset>
<?= $this->Form->button(__('Submit')) ?>
<?= $this->Form->end() ?>
</div>



Result: In the generated html there is no input for name at all.

rrd


2014. szeptember 30., kedd 14:33:24 UTC+2 időpontban Radharadhya Dasa a következőt írta:
Hi,

I am trying to make an autocomplete input work on Cake 3.0

The current 3.0 cookbook gives a code example here : http://book.cakephp.org/3.0/en/core-libraries/helpers/form.html

If I try it I get an error message:
Fatal error: Declaration of App\View\Widget\Autocomplete::render() must be compatible with Cake\View\Widget\WidgetInterface::render(array $data, Cake\View\Form\ContextInterface $context) in /home/rrd/public_html/sanga/src/View/Widget/Autocomplete.php on line 6

If I change my render function to
public function render(array $data, Cake\View\Form\ContextInterface $context) {

Still I get the same error message.

Any idea how to solve this?

rrd

--
Like Us on FaceBook https://www.facebook.com/CakePHP
Find us on Twitter http://twitter.com/CakePHP

---
You received this message because you are subscribed to the Google Groups "CakePHP" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cake-php+unsubscribe@googlegroups.com.
To post to this group, send email to cake-php@googlegroups.com.
Visit this group at http://groups.google.com/group/cake-php.
For more options, visit https://groups.google.com/d/optout.

No comments: