我一直在研究Apress的书“Pro Zend Framework Techniques”。我试图在视图上渲染表单。
表单名为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();
}
}
最后我在我的bootstrap中有以下自动加载器。
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
或者,如果您只是阅读它:致命错误:第23行/home/www-data/zend.danielgroves.net/htdocs/application/controllers/BugController.php中找不到类'Form_BugReport'< / p>
关于我做错了什么的想法,以及如何解决这个问题?
修改
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 */
答案 0 :(得分:2)
你有一个小错字。错误在您的表单中。你在第9行的“this”之前错过了一个$。祝你好运,你的项目的其余部分,对于初学者来说这是一本非常好的书,但它有一些小错误。有关详细信息,请访问编辑网站: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);
答案 1 :(得分:1)
尝试使用:$frmBugReport = new Form_BugReportForm();
您使用的是错误的班级名称。