【问题标题】:zend framework form not showingzend 框架表单未显示
【发布时间】:2011-06-23 05:26:05
【问题描述】:

我开始使用 zend 框架。我遇到了表单的这个问题。 在浏览器中打开 localhost/item/create 时,只显示 hello,不呈现表单。

我做了什么: 我用zf.sh create controller item 在我的项目中创建了一个控制器 然后zf.sh create action create item。 在库文件夹中,创建了 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 显示在浏览器中,其余的表单不显示。谁能指出我做错了什么。 谢谢

【问题讨论】:

  • 您是否查看过 html 源代码(在您的浏览器中)?

标签: php oop model-view-controller zend-framework


【解决方案1】:

我认为原因是您覆盖了 Zend_Form::__constructor()。扩展 Zend_Form 的表单应该使用 init() 函数来初始化它们的元素,而不是 __constructor()

【讨论】:

    【解决方案2】:

    创建表单元素的最佳位置是覆盖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”。从内存中,模块自动加载器将“表单”名称部分转换为模块中的“表单”目录。

    但我确实注意到您没有使用模块自动加载器(或者至少,您没有证明您在使用)


    关于模块自动加载器...

    引导进程使用配置的appnamespace 值(通常是“应用程序”)和基本路径“应用程序”注册模块自动加载器。

    模块自动加载器会自动为从配置的命名空间(例如“Application_”)开始的类注册一些类名/路径映射。这包括表格。

    基本上,这意味着您可以在application/forms/MyForm.php 下创建类名Application_Form_MyForm 的表单,自动加载器会找到它。

    打开“Zend_Application_Module_Autoloader”查看完整列表。

    【讨论】:

    • @Marcin 这次轮到我包含代码了。不过,你仍然击败了我;)
    • 你们这么快的反应真是太棒了! @Phil Brown 能否详细说明自动加载器部分,我是一名学习者;)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-08-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-10-14
    • 1970-01-01
    相关资源
    最近更新 更多