gpt4 book ai didi

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

转载 作者:可可西里 更新时间:2023-10-31 22:10:01 33 4
gpt4 key购买 nike

我在使用 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/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com