我试图将我的登录表单放在layout.phtml中的标题中
我在Application / Module.php
中创建了我的表单对象<?php
namespace Application;
use Zend\Mvc\ModuleRouteListener;
use Zend\Mvc\MvcEvent;
use Users\Form\LoginForm;
class Module
{
public function onBootstrap(MvcEvent $e)
{
.
.
.
$form = new LoginForm();
$form->get('submit')->setValue('Login');
$application = $e->getParam('application');
$viewModel = $application->getMvcEvent()->getViewModel();
$viewModel->form = $form;
}
.
.
.
}
这样每个页面都可以使用该表单(因为layout.phtml用于呈现每个页面)
对于我的layout.phtml文件:
<?php echo $this->doctype(); ?>
<?php
$form = $this->layout()->form;
//\Zend\Debug\Debug::dump($form);
$form->prepare();
$form->setAttribute('action', $this->url(NULL, array('controller' => 'Users', 'action' => 'login')));
?>
<html>
<head>
<?php echo $this->headTitle('Budget') ?>
<?php echo $this->headMeta()
->appendName('viewport', 'width=device-width, initial-scale=1, maximum-scale=1')
->appendHttpEquiv('X-UA-Compatible', 'IE=edge') ?>
<?php echo $this->headLink(array('rel' => 'shortcut icon', 'type' => 'image/vnd.microsoft.icon', 'href' => $this->basePath() . '/img/favicon.ico'))
->prependStylesheet($this->basePath() . '/css/style.css')
->prependStylesheet($this->basePath() . '/css/bootstrap-theme.min.css')
->prependStylesheet($this->basePath() . '/css/bootstrap.min.css') ?>
</head>
<body>
<div class="header">
<nav class="navbar navbar-default" role="navigation">
<div class="container-fluid">
<!-- Brand and toggle get grouped for better mobile display -->
<div class="navbar-header">
<button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1">
<span class="sr-only">Toggle navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a href="/liftshare/" class="navbar-brand">Lift share</a></div>
<!-- Collect the nav links, forms, and other content for toggling -->
<div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
<ul class="nav navbar-nav">
<li><a href="/liftshare/search">Search</a></li>
<li><a href="/liftshare/offers">Offer a lift</a></li>
</ul>
<ul class="nav navbar-nav navbar-right">
<?php echo $this->form()
->setAttribute('class', 'navbar-form')
->setAttribute('role', 'form')
->openTag($form); ?>
<div class="form-group">
<?php echo $this->formLabel($form->get('email')
->setLabelAttributes(array('id' => 'login_email'))
->setLabelAttributes(array('class' => 'sr-only'))
->setLabelAttributes(array('placeholder' => 'Enter email'))
); ?>
<input class="form-control" id="exampleInputEmail2" placeholder="Enter email" type="email">
</div>
<div class="form-group">
<label class="sr-only" for="exampleInputPassword2">Password</label>
<input class="form-control" id="exampleInputPassword2" placeholder="Password" type="password">
</div>
<button type="submit" class="btn btn-default">Sign in</button>
<?php echo $this->form()->closeTag(); ?>
</ul></div><!-- /.navbar-collapse -->
</div>
</nav>
</div>
<?php echo $this->content; ?>
<?php echo $this->inlineScript() ?>
<!-- Scripts -->
<?php echo $this->headScript()
->prependFile($this->basePath() . '/js/bootstrap.min.js')
->prependFile($this->basePath() . '/js/jquery.min.js')
->prependFile($this->basePath() . '/js/respond.min.js', 'text/javascript', array('conditional' => 'lt IE 9',))
->prependFile($this->basePath() . '/js/html5shiv.js','text/javascript', array('conditional' => 'lt IE 9',))
; ?>
</body>
</html>
我收到了错误 - Fatal error: Call to undefined method Zend\Form\View\Helper\Form::setAttribute() in /var/www/budget_zend/module/Application/view/layout/layout.phtml on line 55
据我所知,我的其他视图中可用的表单助手可能在布局视图中不可用。但是如何在layout.phtml文件中放置一个表单呢?另外,似乎有点奇怪,我必须以不同的方式定义它 - 例如,我在某些时候想要将表单从标题(在layout.phtml文件中)移动到主要内容区域 - 这意味着我需要在后端更改内容以进行前端更新。似乎不对。有人可以告诉我应该怎么做。
由于
答案 0 :(得分:1)
我了解在我的其他视图中可用的表单助手可能在布局视图中不可用
事实并非如此。布局和视图可以访问完全相同的视图助手。
我认为您将表单视图助手($this->form()
)与您的登录表单($this->form
或$form
的实例混淆,因为您已将其分配给该变量)。表单 view helper 没有setAttribute()
方法,因此出错。你可能意味着更像这样的东西:
$form->setAttribute('class', 'navbar-form')
->setAttribute('role', 'form');
echo $this->form()->openTag($form);
答案 1 :(得分:0)
我认为编写自定义视图助手然后每次都通过引导事件分配表格更清晰。
假设您将ViewHelper php文件放在Application/src/View/YourFormHelper.php
中,您需要在module_config.php
内配置它,如下所示:
'view_helpers' => array(
'invokables'=> array(
'yourFormHelper' => 'Application\View\Helper\YourFormHelper',
),
),
如果你的表单需要一个服务,你可能会想要创建一个工厂,注入那些而不是只有一个没有依赖的invokable。
在View Helper中,只需像往常一样配置表单。但请记住,您需要扩展AbstractHelper
并实施__invoke()
方法。
namespace Application\View\Helper;
use Zend\View\Helper\AbstractHelper;
class PageChunk extends AbstractHelper
{
public function __invoke()
{
//build form and add elements/inputfilters
return $form;
}
}
你现在可以实现某种渲染,所以它只会返回表格html标签等。
出于解释原因,我们只需将表单附加到我们的布局中,表格应正确显示在布局中。
// within your layout.phtml
...
$form = $this->yourFormHelper();
...
// open tag/close tag, formRow etc