I'm having a problem rendering a form in view using ZF2. I'm following "Zend Framework 2.0 by Example" book by the word. Problem is, when rendering the form in the correspondent view, the following error pops out:
A plugin by the name "form" was not found in the plugin manager Zend\View\HelperPluginManager
I've ran across all types of errors, both here and in the ZF forums, but i couldn't find an answer to my question, so i'm all outta options.
Here is the relevant files:
// 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(); ?>
It seems the form plugin for the view isn't getting loaded, but I have no idea on how to load it or any other way to surpass this problem.
Edit file modules.config.php which can be found under the config/ directory and add following line:
'Zend\Form'
Like this:
return [
'Zend\Form',
'Zend\Db',
'Zend\Router',
'Zend\Validator',
'Application',
];
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With