Cake PHP:使用Form helper时出现未定义的错误

时间:2011-12-31 09:18:19

标签: php cakephp

我是CakePHP的新手。我只是试图在视图上创建一个带有文本框和提交按钮的简单表单,并使用控制器上的帖子数据显示文本。

我的观看代码:

<?php echo $form->create(null, array('action' => 'index'));?>
<fieldset><legend>Enter Your Name</legend><?php echo $form->input('name'); ?></fieldset>
<?php echo $form->end('Go');?> 

我的控制器代码:

<?php
class UsersController extends AppController  {
        var $name = 'Users';
        var $uses = array();
        var $helpers = array('Html','Form');

        function index() {
                if ( !empty($this->data) ) {
                        echo $this->data['name'];
                        $this->autoRender = false;
                }
        }
}

?>

我收到了错误,

Notice (8): Undefined variable: form [APP/View/Users/index.ctp, line 1]
Code Context

include - APP/View/Users/index.ctp, line 1
View::_render() - CORE/Cake/View/View.php, line 598
View::render() - CORE/Cake/View/View.php, line 365
Controller::render() - CORE/Cake/Controller/Controller.php, line 900
Dispatcher::_invoke() - CORE/Cake/Routing/Dispatcher.php, line 114
Dispatcher::dispatch() - CORE/Cake/Routing/Dispatcher.php, line 89
[main] - APP/webroot/index.php, line 96

这有什么问题。

提前致谢。

2 个答案:

答案 0 :(得分:4)

$form->method来自旧版本的CakePHP。在新版本中,表单助手为$this->Form->method。因此,只需在代码中替换这些实例:

<?php echo $this->Form->create(null, array('action' => 'index'));?>
<fieldset><legend>Enter Your Name</legend><?php echo $this->Form->input('name'); ?></fieldset>
<?php echo $this->Form->end('Go');?> 

答案 1 :(得分:1)

喜欢这个

<div class="login"> 
<h2>Login</h2>     
    <?php 

    echo $this->Form->create('User', array('action' => 'login'));?> 
        <?php echo $this->Form->input('username');?> 
        <?php echo $this->Form->input('password');?> 
        <?php echo $this->Form->submit('Login');?> 
    <?php echo $this->Form->end(); ?> 
</div>