zend框架表单不显示
我正在开始使用 zend 框架。我在表单中遇到了这个问题。 在浏览器中打开 localhost/item/create 时,仅显示 hello,不呈现表单。
我做了什么: 我使用 zf.sh create control item
在我的项目中创建了一个控制器 然后zf.sh create操作创建项目
。 在库文件夹内,创建的 App->forms->ItemEditor.php
<?php
class App_forms_ItemEditor extends Zend_Form
{
public function __construct()
{
$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);
}
}
在 application.ini 文件中声明其名称空间
autoloaderNamespaces[] = "App_"
在views->scripts->item->create.phtml 中执行此操作:
hello
<?php
echo $this->form->render();
?>
然后在 ItemController.php 内:
public function createAction()
{
$this->view->form=new App_forms_ItemEditor();
}
浏览器中仅显示 hello,而不会显示表单的其余部分。谁能指出我做错了什么。 谢谢
i am getting started with zend framework.I have encountered this problem with forms.
when opening localhost/item/create in browser, only the hello is shown, form isn't rendered.
what i did:
i created a controller in my project with zf.sh create controller item
then zf.sh create action create item
.
Inside library folder, created App->forms->ItemEditor.php
<?php
class App_forms_ItemEditor extends Zend_Form
{
public function __construct()
{
$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);
}
}
declared its namespace in application.ini file
autoloaderNamespaces[] = "App_"
Inside views->scripts->item->create.phtml did this:
hello
<?php
echo $this->form->render();
?>
and then inside ItemController.php :
public function createAction()
{
$this->view->form=new App_forms_ItemEditor();
}
only the hello is shown in the browser, rest of the form isnt shown. can anyone point out what i'm doing wrong.
thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为原因是你覆盖了 Zend_Form::__constructor()。扩展 Zend_Form 的表单应该使用
init()
函数来初始化其元素,而不是__constructor()
。I think the reason is that you overwriting Zend_Form::__constructor(). Forms that extend Zend_Form should use
init()
function to initialize their elements, not__constructor()
.创建表单元素的最佳位置是重写
init()
方法,例如,如果您劫持
Zend_Form::__construct()
,您至少应该接受相同的参数并使用parent::__construct()
传递它们另外,我对类名的“form”部分中的小写“f”有点不确定。模块自动加载器根据内存将“Form”名称部分转换为模块中的“forms”目录。
不过,我确实注意到您没有使用模块自动加载器(或者至少,您没有表明您正在使用
模块自动加载器)关于模块自动加载器...
引导进程使用配置的
appnamespace 值(通常为“应用程序”),基本路径为“应用程序”。
模块自动加载器会自动为以配置的命名空间(例如“Application_”)开头的类注册一些类名/路径映射。这包括表格。
基本上,这意味着您可以在
application/forms/MyForm.php
下使用类名Application_Form_MyForm
创建表单,自动加载器会找到它。打开“Zend_Application_Module_Autoloader”查看完整列表。
The best place to create your form elements is to override the
init()
method, egIf you hijack
Zend_Form::__construct()
, you should at least accept the same arguments and pass them on usingparent::__construct()
Also, I'm a little unsure about the lowercase "f" in your class name's "form" part. From memory, the module autoloader translates a "Form" name part to a "forms" directory in your module.
I do note however that you're not using the module autoloader (or at least, you haven't shown that you are)
Regarding module autoloaders...
The bootstrap process registers a module autoloader using the configured
appnamespace
value (usually "Application") with base path "application".A module autoloader automatically registers some class name / path mappings for classes starting with the configured namespace (eg "Application_"). This includes forms.
Basically, it means you can create forms under
application/forms/MyForm.php
with classnameApplication_Form_MyForm
and the autoloader will find it.Open up `Zend_Application_Module_Autoloader" to see the full list.