我正在尝试实现一个表单,供学生在我的网站上注册,并为学校提供自动完成控件。为此,我访问了CakePHP 3.0引用并找到了tutorial to achieve this 。我的问题是,他们没有明确说明如何检索自动完成的数据(在本例中为学校名称数组),因此我无法使其工作。 这是我的代码:
注册.ctp
<h1>Registration</h1><br>
<?php echo $this->Form->create('User', array('action' => 'register', 'context' => ['validator' => 'registerValidator'], 'templates' => ['autocomplete' => '<input id="school" name="{{name}}"{{attrs}}>'])); ?>
<?php echo $this->Form->input('fname', array('label' => 'First Name *', 'required' => true, 'placeholder' => 'First Name')); ?>
<?php echo $this->Form->input('sname', array('label' => 'Last Name *', 'required' => true, 'placeholder' => 'Last Name')); ?>
<?php echo $this->Form->input('country', array('options' => array('Select...', 'India', 'USA', 'Canada'), 'placeholder' => 'Select')); ?>
<?php //echo $this->Form->input('school', array('label' => 'School *', 'options' => array('Select...', 'School A', 'School B', 'School C'))); ?>
<?php
$this->Form->addWidget(
'autocomplete', ['Autocomplete', 'App\View\Widget\Autocomplete']
);
$options = array("Arena", "Bambu", "Canela", "Perlita");
//echo $this->Form->input('School', ['type' => 'autocomplete']);
echo $this->Form->input('search', ['type' => 'autocomplete', 'label' => 'School *', 'id' => 'school', 'required' => true, 'autocomplete' => 'off', 'placeholder' => 'School']);
//echo $this->Form->autocomplete('search', $options);
?>
<?php echo $this->Form->input('province_region', array('label' => 'Province/Region', 'placeholder' => 'Province/Region')); ?>
<?php echo $this->Form->input('city_town', array('label' => 'City/Town', 'placeholder' => 'City/Town')); ?>
<?php echo $this->Form->input('email', array('label' => 'E-mail *', 'required' => true, 'placeholder' => 'Email')); ?>
<?php echo $this->Form->input('birth_dt', ['type' => 'date', 'label' => 'Date of birth *', 'minYear' => date('Y') - 100, 'maxYear' => date('Y'), 'empty' => '-', 'default' => '', 'required' => true]);
?>
<?php echo $this->Form->input('password', array('label' => 'Password *', 'required' => true, 'placeholder' => 'Password')); ?>
<?php
echo $this->Form->input('pwd_repeat', array('label' => 'Confirm Password *', 'type' => 'password', 'required' => true, 'placeholder' => 'Repeat Password'));
echo $this->Form->submit('Send');
?>
自动完成.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 [];
return [$data['name']];
}
}
?>
UsersController.php
<?php
namespace App\Controller;
use App\Controller\AppController;
use Cake\ORM\TableRegistry;
use Cake\Mailer\Email;
use Cake\Routing\Router;
use Cake\Event\Event;
use Cake\Validation\Validator;
class UsersController extends AppController {
public $helpers = [
'Form' => [
'widgets' => [
'autocomplete' => ['App\View\Widget\Autocomplete']
]
]
];
....
这是输出:
我们将非常感谢任何类型的帮助。预先感谢您的帮助。
最佳答案
自动完成
输入将仅显示您之前使用同一浏览器输入的值。
The autocomplete attribute specifies whether or not an input field should have autocomplete enabled.
Autocomplete allows the browser to predict the value. When a user starts to type in a field, the browser should display options to fill in the field, based on earlier typed values.
来源:http://www.w3schools.com/tags/att_input_autocomplete.asp
您希望像搜索引擎那样显示建议。要检索数据,您需要使用 JavaScript。
关于php - CakePHP 3.X 中的自动完成小部件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33225121/