【问题标题】:Zend Framework 2 - how to use form helpers in layout.phtmlZend Framework 2 - 如何在 layout.phtml 中使用表单助手
【发布时间】:2014-09-07 02:44:21
【问题描述】:

我正在尝试将我的登录表单放在位于 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 文件中)移动到主要内容区域 - 这意味着我需要在后端更改内容以进行前端更新。似乎不对。有人可以告诉我我应该怎么做。

谢谢

【问题讨论】:

    标签: php zend-framework zend-framework2


    【解决方案1】:

    我了解在我的其他视图中可用的表单助手可能在布局视图中不可用

    事实并非如此。布局和视图可以访问完全相同的视图助手。

    我认为您将表单视图助手 ($this-&gt;form()) 与登录表单的实例($this-&gt;form$form 混淆,因为您已将其分配给该变量)。 view helper 表单没有setAttribute() 方法,因此出现错误。你的意思可能更像这样:

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

    【讨论】:

      【解决方案2】:

      我认为编写自定义视图助手然后每次通过引导事件分配表单会更干净。

      假设您将 ViewHelper php 文件放在 Application/src/View/YourFormHelper.php 中,您需要在 module_config.php 中配置它,如下所示:

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

      如果您的表单需要服务,您可能希望创建一个注入这些服务的工厂,而不是仅仅拥有一个没有依赖关系的可调用对象。

      在您的 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 
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-01-18
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多