简体   繁体   English

Zend Framework 2-如何在layout.phtml中使用表单助手

[英]Zend Framework 2 - how to use form helpers in layout.phtml

I'm trying to put my login form in the header which resides within layout.phtml 我正在尝试将我的登录表单放在layout.phtml中的标头中

I've created my form object in Application/Module.php 我已经在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;
  }
.
.
.
}

This is so that the form is available for every page (as layout.phtml is used to render every page) 这样一来,该表单可用于每个页面(因为layout.phtml用于呈现每个页面)

For my layout.phtml file: 对于我的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>

I'm getting the error - 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 我遇到了错误- 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

I understand that the form helpers that were available in my other views are perhaps not available in the layout view. 我了解其他视图中可用的表单助手可能在布局视图中不可用。 But how do I put a form in the layout.phtml file then? 但是,如何将表单放入layout.phtml文件中呢? Also, seems a bit strange that I have to define this in different ways - say, for example, I at some point want to move the form from the header (within the layout.phtml file) to the main content area - this means I need to change stuff at the backend to make a front end update. 另外,我必须以不同的方式定义它似乎有些奇怪-例如,例如,我有时希望将表单从标题(在layout.phtml文件中)移动到主要内容区域-这意味着我需要在后端进行更改以进行前端更新。 Doesn't seem right. 似乎不正确。 Can someone please inform me how I should be doing things. 有人可以告诉我我应该怎么做。

Thanks 谢谢

I understand that the form helpers that were available in my other views are perhaps not available in the layout view 我了解其他视图中可用的表单助手可能在布局视图中不可用

This is not the case. 不是这种情况。 Layouts and views can access exactly the same view helpers. 布局和视图可以访问完全相同的视图助手。

I think you are confusing the form view helper ( $this->form() ) with the instance of your Login form ( $this->form , or $form since you've assigned it to that variable). 我认为您正在将表单视图助手( $this->form() )与登录表单的实例( $this->form$form混淆,因为您已将其分配给该变量)。 The form view helper doesn't have a setAttribute() method, hence the error. 表单视图帮助程序没有setAttribute()方法,因此会出现错误。 You probably mean something more like this: 您可能表示的是更多类似这样的内容:

$form->setAttribute('class', 'navbar-form')
     ->setAttribute('role', 'form');

echo $this->form()->openTag($form);

I think it's cleaner to write a custom view helper then assigning the Form trough the bootstrap event every time. 我认为编写自定义视图助手然后每次都通过bootstrap事件分配Form会更干净。

Assuming you'd place the ViewHelper php file in Application/src/View/YourFormHelper.php you would need to configure it within the module_config.php like so: 假设您将ViewHelper php文件放置在Application/src/View/YourFormHelper.php ,则需要在module_config.php对其进行配置,如下所示:

'view_helpers' => array(
    'invokables'=> array(
        'yourFormHelper' => 'Application\View\Helper\YourFormHelper',
    ),
),

If your Form requires a service you might asweel want to create a factory which inject's those rather then just having a invokable without dependencies. 如果您的表格需要服务,您可能会想创建一个工厂来注入那些工厂,而不是仅仅拥有一个没有依赖项的可调用项。

Within your View Helper just configure your forms as usual. 在View Helper中,只需照常配置表单即可。 But keep in mind You'll need to extend the AbstractHelper and implement the __invoke() method. 但是请记住,您将需要扩展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;
    }
}

You now could implement some kind of rendering so It would just return the form html tag's etc. 您现在可以实现某种形式的呈现,因此它将仅返回html标签的形式等。

For explanatory reasons we'll just attach the form in our layout like so and the form should be displayed in the layout correctly. 出于解释原因,我们将像这样将表单附加到布局中,并且表单应正确显示在布局中。

// within your layout.phtml
...
$form = $this->yourFormHelper();
...
// open tag/close tag, formRow etc 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM