zend框架表单不显示

发布于 2024-10-16 12:18:11 字数 1246 浏览 2 评论 0原文

我正在开始使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

紫瑟鸿黎 2024-10-23 12:18:11

我认为原因是你覆盖了 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().

揽清风入怀 2024-10-23 12:18:11

创建表单元素的最佳位置是重写 init() 方法,例如,

class App_forms_ItemEditor extends Zend_Form
{
    public function init()
    {
        $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);

    }
}

如果您劫持 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, eg

class App_forms_ItemEditor extends Zend_Form
{
    public function init()
    {
        $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);

    }
}

If you hijack Zend_Form::__construct(), you should at least accept the same arguments and pass them on using parent::__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 classname Application_Form_MyForm and the autoloader will find it.

Open up `Zend_Application_Module_Autoloader" to see the full list.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文