我在使用ZF2在视图中呈现表单时遇到问题。我正在按照“Zend Framework 2.0 by Example”一书的说法。问题是,在对应视图中渲染表单时,弹出以下错误:
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(); ?>
看起来视图的表单插件没有加载,但我不知道如何加载它或任何其他方式来超越这个问题。
答案 0 :(得分:8)
编辑文件 modules.config.php ,可以在 config / 目录下找到并添加以下行:
'Zend\Form'
像这样:
return [
'Zend\Form',
'Zend\Db',
'Zend\Router',
'Zend\Validator',
'Application',
];
答案 1 :(得分:0)
修改 modules.config.php
实际路径(在我的项目中)/var/www/html/zf2-tutorial/config/modules.config.php
添加 Zend的\表”, 在返回数组( )
我的档案看起来像这样
返回数组(
'Zend\Form',
'Zend\Router',
'Zend\Validator',
'Application',
'BookList'
);
答案 2 :(得分:-1)
Viewhelper需要更新以下内容:
$formHelper = new Zend\Form\View\Helper\Form();
$labelHelper = new Zend\Form\View\Helper\FormLabel();
$elementHelper = new Zend\Form\View\Helper\FormElement();
$inputHelper = new Zend\Form\View\Helper\FormInput();
$textareaHelper = new Zend\Form\View\Helper\FormTextarea();
$errorHelper = new Zend\Form\View\Helper\FormElementErrors();
echo $formHelper->openTag($form);
?>
<dl class="zend_form">
<dt><?php
$name = $form->get('name');
echo $labelHelper->openTag($name) . $name->getLabel();
echo $labelHelper->closeTag($name);
?>
</dt>
<dd><?php
echo $inputHelper($name);
echo $errorHelper($name);
?></dd>
它会显示你想要的东西。