CakePHP 2.4:不需要的预填充表单数据


CakePHP 2.4: Unwanted pre-filled form data

我有一个添加新用户的表单。只有已登录的管理员才能访问此表单。不幸的是,管理员的用户名和密码被填写到表单字段中,这些表单字段应该是完全清晰的。一件非常奇怪的事情是:用户名被打印到生日字段中!

我真的无法解释自己是如何工作的。而且我在WWW中找不到遇到相同问题的人的任何帖子-我只找到了有关所需预填写表单数据的问题和答案。

这是视图/用户/add.ctp

<h1>Add a new Member</h1>
<?php echo $this->Form->create('User', array('url' => BASE_URL.'/users/add', 'action'=>'post')); ?>
<table class="form">
     <tr><td>Username:</td><td><?php echo $this->Form->input('User.username', array('label' => false, 'div' => false, 'value' => ''));?></td></tr>
     <tr><td>Name:</td><td><?php echo $this->Form->input('User.name', array('label' => false, 'div' => false, 'value' => ''));?></td></tr>
     <tr><td>Lastname:</td><td><?php echo $this->Form->input('User.lastname', array('label' => false, 'div' => false, 'value' => ''));?></td></tr>
     <tr><td>E-Mail:</td><td><?php echo $this->Form->input('User.email', array('label' => false, 'div' => false, 'value' => ''));?></td></tr>
     <tr><td>Birthday:</td><td><?php echo $this->Form->input('User.birth', array('label' => false, 'div' => false, 'value' => ''));?></td></tr>
     <tr><td>Password:</td><td><?php echo $this->Form->input('User.password', array('label' => false, 'div' => false, 'value' => ''));?></td></tr>
</table>
<?php 
     echo $this->Form->submit('Submit', array('formnovalidate' => true));
     echo $this->Form->end();
?>

这是控制器/用户控制器.php

public function add() {
    $this->layout = 'admin';
    if ($this->request->is('post')) {
        // Saving the data
        $this->User->create();
        if ($this->User->save($this->request->data)) {
            $this->Session->setFlash(__('Data saved.'));
            return $this->redirect(array('action' => 'view'));
        }
        $this->Session->setFlash(__('Data could not be saved.'));
    }
}

顺便说一句:保存工作正常。当然,管理员是对象用户,要添加的新成员也是如此。我想,问题就在这里,但我真的不知道...我整天都在思考这个问题:(有人知道该怎么做吗?

提前谢谢。

不是你的浏览器吗?(首次键入时保存的用户名/密码)

因此,您可以打开自动完成功能。

<?php echo $this->Form->create('User', array('url' => BASE_URL.'/users/add', 'action'=>'post', 'autocomplete' => 'off')); ?>

此选项 => "自动完成" => "关闭"

检查你的$this->data .

CakePHP 使用在那里找到的数据自动填写表单,因为它猜测这是用户已经提交的数据。

在您的示例中,如果$this->data['User']['birth']有一些值,则应在"生日"输入中显示该值。