jjzjj

PHP Zend Framework 2 - 呈现表单时出现问题 - "form plugin not found"

coder 2024-04-12 原文

我在使用 ZF2 呈现表单时遇到问题。我正在逐字逐句地阅读“Zend Framework 2.0 by Example”一书。问题是,在相应 View 中呈现表单时,弹出以下错误:

A plugin by the name "form" was not found in the plugin manager Zend\View\HelperPluginManager

我在这里和 ZF 论坛上遇到过所有类型的错误,但我找不到我的问题的答案,所以我别无选择。

相关文件如下:

// Module.php
namespace Users;

use Zend\ModuleManager\Feature\AutoloaderProviderInterface;
use Zend\ModuleManager\Feature\ConfigProviderInterface;

class Module implements AutoloaderProviderInterface, ConfigProviderInterface {

    public function getAutoloaderConfig() {
        return [
            'Zend\Loader\ClassMapAutoloader' => [
                __DIR__ . '/autoload_classmap.php',
            ],
            'Zend\Loader\StandardAutoloader' => [
                'namespaces' => [
                    __NAMESPACE__ => __DIR__ . '/src/' . __NAMESPACE__,
                ],
            ],
        ];
    }

    public function getConfig() {
        return include __DIR__ . '/config/module.config.php';
    }

}


//module.config.php
namespace Users;

return [
    'controllers' => [
        'invokables' => [
            'Users\Controller\Index' => 'Users\Controller\IndexController',
            'Users\Controller\Register' => 'Users\Controller\RegisterController',
        ]
    ],
    'router' => [
        'routes' => [
            'users' => [
                'type' => 'literal',
                'options' => [
                    'route' => '/users',
                    'defaults' => [
                        'controller' => 'Users\Controller\Index',
                        'action' => 'index',
                    ],
                ],
                'may_terminate' => true,
                'child_routes' => [
                    'register' => [
                        'type' => 'segment',
                        'options' => [
                            'route' => '/register[/:action]',
                            'constraints' => [
                                'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
                            ],
                            'defaults' => [
                                'controller' => 'Users\Controller\Register',
                                'action' => 'index',
                            ],
                        ],
                    ],
                    'index' => [
                        'type' => 'segment',
                        'options' => [
                            'route' => '/index[/:action]',
                            'constraints' => [
                                'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
                            ],
                            'defaults' => [
                                'controller' => 'Users\Controller\Index',
                                'action' => 'index',
                            ],
                        ],
                    ],
                ],
            ],
        ],
    ],
    'view_manager' => [
        'template_path_stack' => [
            'users' => __DIR__ . '/../view',
        ],
    ],
    'service_manager' => array(
        'factories' => array(
            'convertercontent' => 'Zend\Form\Factory'
        ),
    ),
];

//RegisterForm.php
namespace Users\Form;

use Zend\Form\Form;
use Zend\Validator\EmailAddress;

class RegisterForm extends Form {
    public function __construct($name = null) {
        parent::__construct('register');
        $this->setAttribute('method', 'post');
        $this->setAttribute('enctype', 'multipart/form-data');

        $this->add([
            'name' => 'name',
            'attributes' => [
                'type' => 'text',
            ],
            'options' => [
                'label' => 'Full Name',
            ]
        ]);

        $this->add([
            'name' => 'email',
            'attributes' => [
                'type' => 'email',
                'required' => 'required',
                'id' => 'email',
            ],
            'options' => [
                'label' => 'Password',
            ],
            'filters' => [
                [
                    'name' => 'StringTrim',
                ],
            ],
            'validators' => [
                [
                    'name' => 'EmailAddress',
                    'options' => [
                        'messages' => [
                            EmailAddress::INVALID_FORMAT => 'Email address format is invalid',
                        ],
                    ],

                ],
            ],
        ]);

        $this->add([
            'name' => 'password',
            'attributes' => [
                'type' => 'password',
                'required' => 'required',
                'id' => 'password',
            ],
            'options' => [
                'label' => 'Password',
            ],
            'filters' => [
                [
                    'name' => 'StringTrim',
                ],
            ],
        ]);

        $this->add([
            'name' => 'confirm_password',
            'attributes' => [
                'type' => 'password',
                'required' => 'required',
                'id' => 'confirm_password',
            ],
            'options' => [
                'label' => 'Confirm password',
            ],
            'filters' => [
                [
                    'name' => 'StringTrim',
                ],
            ],
        ]);

        $this->add([
            'name' => 'submit',
            'attributes' => [
                'type' => 'button',
                'id' => 'submit',
            ],
            'options' => [
                'label' => 'Submit',
            ],
        ]);
    }
}

//RegisterController.php
namespace Users\Controller;

use Zend\Mvc\Controller\AbstractActionController;
use Zend\View\Model\ViewModel;
use Users\Form\RegisterForm;

class RegisterController extends AbstractActionController {

    public function indexAction(){        
        $form = new RegisterForm();
        $viewModel = new ViewModel(array('form' => $form));
        return $viewModel;
    }

    public function confirmAction() {
        $viewModel = new ViewModel();
        return $viewModel;
    }
}

// register view index "index.phtml" (form page)
<section class="register">
    <h2>Register</h2>
    <?php if ($this->error): ?>
        <p class="error">
            There were one or more issues with your submission.
            Please correct them as
            indicated below.
        </p>
    <?php endif
    ?>

        <?php        
        $form = $this->form;    
        $form->setAttribute('action', $this->url());    
        $form->prepare();    

    $form = $this->form;    
    $form->setAttribute('action', $this->url(NULL, array('controller' => 'Register', 'action' => '    process')));
    $form->setAttribute('method', 'post');
    echo $this->form()->openTag($form);
    ?> 

    <dl class="zend_form">
        <dt><?php echo $this->formLabel($form->get('name')); ?></dt>
        <dd><?php
            echo $this->formElement($form->get('name'));
            echo $this->formElementErrors($form->get('name'));
            ?></dd>
        <dt><?php echo $this->formLabel($form->get('email')); ?></
        dt>
        <dd><?php
            echo $this->formElement($form->get('email'));
            echo $this->formElementErrors($form->get('email'));
            ?></dd>
        <dt><?php echo $this->formLabel($form->get('password'));
            ?></dt>
        <dd><?php
            echo $this->formElement($form->get('password'));
            echo $this->formElementErrors($form->get('password'));
            ?></dd>
        <dt><?php echo $this->formLabel($form->get('confirm_
password')); ?></dt>
        <dd><?php
            echo $this->formElement($form->get('confirm_password'));
            echo $this->formElementErrors($form->get('confirm_
password'));
            ?></dd>
        <dd><?php
            echo $this->formElement($form->get('submit'));
            echo $this->formElementErrors($form->get('submit'));
            ?></dd>
    </dl>

    <?php echo $this->form()->closeTag(); ?>

似乎没有加载 View 的表单插件,但我不知道如何加载它或任何其他方法来解决这个问题。

最佳答案

编辑 modules.config.php 文件,可以在 config/ 目录下找到并添加以下行:

'Zend\Form'

像这样:

return [
  'Zend\Form',
  'Zend\Db',
  'Zend\Router',
  'Zend\Validator',
  'Application',
];

关于PHP Zend Framework 2 - 呈现表单时出现问题 - "form plugin not found",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38268778/

有关PHP Zend Framework 2 - 呈现表单时出现问题 - "form plugin not found"的更多相关文章

  1. ruby-on-rails - rails : "missing partial" when calling 'render' in RSpec test - 2

    我正在尝试测试是否存在表单。我是Rails新手。我的new.html.erb_spec.rb文件的内容是:require'spec_helper'describe"messages/new.html.erb"doit"shouldrendertheform"dorender'/messages/new.html.erb'reponse.shouldhave_form_putting_to(@message)with_submit_buttonendendView本身,new.html.erb,有代码:当我运行rspec时,它失败了:1)messages/new.html.erbshou

  2. ruby-on-rails - 由于 "wkhtmltopdf",PDFKIT 显然无法正常工作 - 2

    我在从html页面生成PDF时遇到问题。我正在使用PDFkit。在安装它的过程中,我注意到我需要wkhtmltopdf。所以我也安装了它。我做了PDFkit的文档所说的一切......现在我在尝试加载PDF时遇到了这个错误。这里是错误:commandfailed:"/usr/local/bin/wkhtmltopdf""--margin-right""0.75in""--page-size""Letter""--margin-top""0.75in""--margin-bottom""0.75in""--encoding""UTF-8""--margin-left""0.75in""-

  3. ruby - ECONNRESET (Whois::ConnectionError) - 尝试在 Ruby 中查询 Whois 时出错 - 2

    我正在用Ruby编写一个简单的程序来检查域列表是否被占用。基本上它循环遍历列表,并使用以下函数进行检查。require'rubygems'require'whois'defcheck_domain(domain)c=Whois::Client.newc.query("google.com").available?end程序不断出错(即使我在google.com中进行硬编码),并打印以下消息。鉴于该程序非常简单,我已经没有什么想法了-有什么建议吗?/Library/Ruby/Gems/1.8/gems/whois-2.0.2/lib/whois/server/adapters/base.

  4. ruby-on-rails - Rails 编辑表单不显示嵌套项 - 2

    我得到了一个包含嵌套链接的表单。编辑时链接字段为空的问题。这是我的表格:Editingkategori{:action=>'update',:id=>@konkurrancer.id})do|f|%>'Trackingurl',:style=>'width:500;'%>'Editkonkurrence'%>|我的konkurrencer模型:has_one:link我的链接模型:classLink我的konkurrancer编辑操作:defedit@konkurrancer=Konkurrancer.find(params[:id])@konkurrancer.link_attrib

  5. ruby - 在 64 位 Snow Leopard 上使用 rvm、postgres 9.0、ruby 1.9.2-p136 安装 pg gem 时出现问题 - 2

    我想为Heroku构建一个Rails3应用程序。他们使用Postgres作为他们的数据库,所以我通过MacPorts安装了postgres9.0。现在我需要一个postgresgem并且共识是出于性能原因你想要pggem。但是我对我得到的错误感到非常困惑当我尝试在rvm下通过geminstall安装pg时。我已经非常明确地指定了所有postgres目录的位置可以找到但仍然无法完成安装:$envARCHFLAGS='-archx86_64'geminstallpg--\--with-pg-config=/opt/local/var/db/postgresql90/defaultdb/po

  6. ruby - 通过 rvm 升级 ruby​​gems 的问题 - 2

    尝试通过RVM将RubyGems升级到版本1.8.10并出现此错误:$rvmrubygemslatestRemovingoldRubygemsfiles...Installingrubygems-1.8.10forruby-1.9.2-p180...ERROR:Errorrunning'GEM_PATH="/Users/foo/.rvm/gems/ruby-1.9.2-p180:/Users/foo/.rvm/gems/ruby-1.9.2-p180@global:/Users/foo/.rvm/gems/ruby-1.9.2-p180:/Users/foo/.rvm/gems/rub

  7. ruby - 检查 "command"的输出应该包含 NilClass 的意外崩溃 - 2

    为了将Cucumber用于命令行脚本,我按照提供的说明安装了arubagem。它在我的Gemfile中,我可以验证是否安装了正确的版本并且我已经包含了require'aruba/cucumber'在'features/env.rb'中为了确保它能正常工作,我写了以下场景:@announceScenario:Testingcucumber/arubaGivenablankslateThentheoutputfrom"ls-la"shouldcontain"drw"假设事情应该失败。它确实失败了,但失败的原因是错误的:@announceScenario:Testingcucumber/ar

  8. ruby - 通过 RVM (OSX Mountain Lion) 安装 Ruby 2.0.0-p247 时遇到问题 - 2

    我的最终目标是安装当前版本的RubyonRails。我在OSXMountainLion上运行。到目前为止,这是我的过程:已安装的RVM$\curl-Lhttps://get.rvm.io|bash-sstable检查已知(我假设已批准)安装$rvmlistknown我看到当前的稳定版本可用[ruby-]2.0.0[-p247]输入命令安装$rvminstall2.0.0-p247注意:我也试过这些安装命令$rvminstallruby-2.0.0-p247$rvminstallruby=2.0.0-p247我很快就无处可去了。结果:$rvminstall2.0.0-p247Search

  9. ruby - Fast-stemmer 安装问题 - 2

    由于fast-stemmer的问题,我很难安装我想要的任何ruby​​gem。我把我得到的错误放在下面。Buildingnativeextensions.Thiscouldtakeawhile...ERROR:Errorinstallingfast-stemmer:ERROR:Failedtobuildgemnativeextension./System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/bin/rubyextconf.rbcreatingMakefilemake"DESTDIR="cleanmake"DESTDIR=

  10. ruby-on-rails - 迷你测试错误 : "NameError: uninitialized constant" - 2

    我遵循MichaelHartl的“RubyonRails教程:学习Web开发”,并创建了检查用户名和电子邮件长度有效性的测试(名称最多50个字符,电子邮件最多255个字符)。test/helpers/application_helper_test.rb的内容是:require'test_helper'classApplicationHelperTest在运行bundleexecraketest时,所有测试都通过了,但我看到以下消息在最后被标记为错误:ERROR["test_full_title_helper",ApplicationHelperTest,1.820016791]test

随机推荐