2012-07-24 122 views
0

我一直在努力通过Apress出版的书“临Zend框架技术”。我试图在视图上呈现表单。致命错误:类“Form_BugReport”未找到

的形式被称为BugReport,其位于forms/BugReportForm.php;这里是文件的内容:

<?php 

class Form_BugReportForm extends Zend_Form 
{ 
    public function init() 
    { 
     // add element: author textbox 
     $author = this->createElement('text', 'author'); 
     $author->setLabel('Enter your name:'); 
     $author->setRequired(TRUE); 
     $author->setAttrib('size', 30); 
     $this->addElement($author); 

     // add element: email textbox 
     $email = $this->createElement('text', 'email'); 
     $email->setLabel('Your email address:'); 
     $email->setRequired(TRUE); 
     $email->addValidator(new Zend_Validate_EmailAddress()); 
     $email->addFilters(array(
     new Zend_Filter_StringTrim(), 
     new Zend_Filter_StringToLower() 
     )); 
     $email = setAttrib('size', 40); 
     $this->addElement($email); 

     // add element: date textbox 
     $date = $this->createElement('text', 'date'); 
     $date->setLabel('Date the issue occurred (dd-mm-yyyy)'); 
     $date->setRequired(TRUE); 
     $date->addValidator(new Zend_Validate_Date('MM-DD-YYYY')); 
     $date->setAttrib('size', 20); 
     $this->addElement($date); 

     // add element: URL textbox 
     $url = $this->createElement('text', 'url'); 
     $url->setLabel('Issue URL:'); 
     $url->setRequired(TRUE); 
     $url->setAttrib('size', 50); 
     $this->addElement($url); 

     // add element: description text area 
     $description = $this->createElement('textarea', 'description'); 
     $description->setLabel('Issue description:'); 
     $description->setRequired(TRUE); 
     $description->setAttrib('cols', 50); 
     $description->setAttrib('rows', 4); 
     $this->addElement($description); 

     // add element: priority select box 
     $priority = $this->createElement('select', 'priority'); 
     $priority->setLabel('Issue priority:'); 
     $priority->setRequired(TRUE); 
     $priority->addMultiOptions(array(
     'low' => 'Low', 
     'med' => 'Medium', 
     'high' => 'High' 
     )); 
     $this->addElement($priority); 

     // add element: status select box 
     $status = $this->createElement('select', 'status'); 
     $status->setLabel('Current status:'); 
     $status->setRequired(TRUE); 
     $status->addMultiOptions(array(
     'new'   => 'New', 
     'in_progress' => 'In Progress', 
     'resolved'  => 'Resolved' 
     )); 
     $this->addElement($status); 

     // add element: submit button 
     $this->addElement('submit', 'submit', array('label' => 'Submit')); 
    } 
} 

这种形式,通过位于BugController控制器控制在/controllers/BugController.php

<?php 

class BugController extends Zend_Controller_Action 
{ 

    public function init() 
    { 
     /* Initialize action controller here */ 
    } 

    public function indexAction() 
    { 
     // action body 
    } 

    public function createAction() 
    { 
     // action body 
    } 

    public function submitAction() 
    { 
     $frmBugReport = new Form_BugReport(); 
     $frmBugReport->setAction('/bug/submit'); 
     $frmBugReport->setMethod('post'); 
     $this->view->form = $frmBugReport(); 
    } 

} 

最后,我在我的引导下自动加载。

protected function _initAutoload() 
{ 
    // Add autoloader empty namespace 
    $autoLoader = Zend_Loader_Autoloader::getInstance(); 
    $resourceLoader = new Zend_Loader_Autoloader_Resource(array(
     'basePath'  => APPLICATION_PATH, 
     'namespace'  => '', 
     'resourceTypes' => array(
     'form' => array(
      'path'  => 'forms/', 
      'namespace' => 'Form_', 
     ) 
    ), 
    )); 

    // Return it so it can be stored by the bootstrap 
    return $autoLoader; 
} 

我正的错误可以在这里看到:http://zend.danielgroves.net/bug/submit

或者,如果你宁愿只是阅读:致命错误:类“Form_BugReport”中找不到的/ home/www数据/ zend.danielgroves.net/htdocs/application/controllers/BugController.php 23行

任何想法,我做了什么错误,以及如何可以解决这个问题?

编辑

ArneRie指出我用错了类名,但遗憾的是这并没有固定的问题。下面是BugController现在的样子:

<?php 

class BugController extends Zend_Controller_Action 
{ 

    public function init() 
    { 
     /* Initialize action controller here */ 
    } 

    public function indexAction() 
    { 
     // action body 
    } 

    public function createAction() 
    { 
     // action body 
    } 

    public function submitAction() 
    { 
     $frmBugReport = new Form_BugReportForm(); 
     $frmBugReport->setAction('/bug/submit'); 
     $frmBugReport->setMethod('post'); 
     $this->view->form = $frmBugReport; 
    } 

} 

这虽然摆脱有问题的错误,一个新的已经很到位。

Parse error: syntax error, unexpected T_OBJECT_OPERATOR in /home/www-data/zend.danielgroves.net/htdocs/application/forms/BugReportForm.php on line 8 

8号线是有趣/* Initialize action controller here */

+1

解析错误是在你的表单类中,而不是你的控制器。您在该行的'this'前面缺少'$'。 – 2012-07-24 13:35:52

+0

谢谢蒂姆,找到并修好了!非常感谢! – 2012-07-24 13:41:01

回答

1

与尝试:$frmBugReport = new Form_BugReportForm();

您使用了错误的类名。

+0

谢谢你的建议,这已经移除了错误,但不幸的是用一个新的替换它,看到我的编辑和我原来的问题的底部。感谢您一直以来的帮助! – 2012-07-24 13:29:27

2

你有一个小错字。该错误在您的表单中。您缺少$之前的“本”上线9:好,你的项目的其余部分运气,这是一个很不错的书对于初学者但也有它的几个小错误。查看编辑器网站获取详细信息:勘误部分中的http://www.apress.com/9781430218791/

<?php 

class Form_BugReportForm extends Zend_Form 
{ 
    public function init() 
    { 
     // add element: author textbox 
//Next line is the line to change (add a $ before the "this") 
     $author = $this->createElement('text', 'author'); 
     $author->setLabel('Enter your name:'); 
     $author->setRequired(TRUE); 
     $author->setAttrib('size', 30); 
     $this->addElement($author); 
相关问题