Zendframework 2 Skeleton应用程序缺少Albumform样式

时间:2014-10-02 11:10:25

标签: php zend-framework2

我目前正在ZF2中重建Skeleton应用程序 在“Zend Framework 2 Documentation,Release 2.3.3”中,有一个用于添加/编辑相册的表单。

在文档中,表单如下所示: zendframework example

我一步一步地遵循了文档,但我的看起来像这样: zendframework example mine

AlbumForm.php:

<?php
namespace Album\Form;

use Zend\Form\Form;

class AlbumForm extends Form
{
    public function __construct($name = null)
    {
        parent::__construct('album');

        $this->add(array(
            'name' => 'id',
            'type' => 'Hidden',
        ));
        $this->add(array(
            'name' => 'title',
            'options' => array(
                'label' => 'Title',
            ),
            'attributes' => array(
                'type' => 'text',
            )
        ));
        $this->add(array(
            'name' => 'artist',
            'options' => array(
                'label' => 'Artist',
            ),
            'attributes' => array(
                'type' => 'text',
            )
        ));
        $this->add(array(
            'name' => 'submit',
            'attributes' => array(
                'type' => 'submit',
                'value' => 'Go',
                'id' => 'submitbutton',
            ),
        ));
    }
}

add.phtml

<?php

$title = 'Add new album';
$this->headTitle($title);
?>

<h1><?= $this->escapeHtml($title); ?></h1>

<?php

$form->setAttribute('action', $this->url('album', array('action' => 'add')));
$form->prepare();

echo $this->form()->openTag($form);
echo $this->formHidden($form->get('id'));
echo $this->formRow($form->get('title'));
echo $this->formRow($form->get('artist'));
echo $this->formSubmit($form->get('submit'));
echo $this->form()->closeTag();

HTML输出:

<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="utf-8">
        <title>Add new album - ZF2 Tutorial</title>
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
        <!-- Le styles -->
        <link href="/css/bootstrap.min.css" media="screen" rel="stylesheet" type="text/css">
<link href="/css/bootstrap-theme.min.css" media="screen" rel="stylesheet" type="text/css">
<link href="/css/style.css" media="screen" rel="stylesheet" type="text/css">
<link href="/img/favicon.ico" rel="shortcut icon" type="image/vnd.microsoft.icon">
        <!-- Scripts -->
        <!--[if lt IE 9]><script type="text/javascript" src="/js/html5shiv.js"></script><![endif]-->
<!--[if lt IE 9]><script type="text/javascript" src="/js/respond.min.js"></script><![endif]-->
<script type="text/javascript" src="/js/jquery.min.js"></script>
<script type="text/javascript" src="/js/bootstrap.min.js"></script>
    </head>
    <body>
        <nav class="navbar navbar-inverse navbar-fixed-top" role="navigation">
            <div class="container">
                <div class="navbar-header">
                    <button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse">
                        <span class="icon-bar"></span>
                        <span class="icon-bar"></span>
                        <span class="icon-bar"></span>
                    </button>
                    <a class="navbar-brand" href="/"><img src="/img/zf2-logo.png" alt="Zend Framework 2"/>&nbsp;Tutorial</a>
                </div>
                <div class="collapse navbar-collapse">
                    <ul class="nav navbar-nav">
                        <li class="active"><a href="/">Home</a></li>
                    </ul>
                </div><!--/.nav-collapse -->
            </div>
        </nav>
        <div class="container">

<h1>Add new album</h1>

<form action="&#x2F;album&#x2F;add" method="POST" name="album" id="album"><input type="hidden" name="id" value=""><label><span>Title</span><input name="title" type="text" value=""></label><label><span>Artist</span><input name="artist" type="text" value=""></label><input name="submit" type="submit" id="submitbutton" value="Add"></form>            <hr>
            <footer>
                <!--<p>&copy; 2005 - 2014 by Zend Technologies Ltd. All rights reserved.</p>-->
            </footer>
        </div> <!-- /container -->
            </body>
</html>

我希望有人知道我的错误是什么......

3 个答案:

答案 0 :(得分:2)

如果您使用bootstrap3,则需要为输入字段添加适当的类 所以你需要改变你的albumform.php,如:

    <?php
namespace Album\Form;

use Zend\Form\Form;

class AlbumForm extends Form
{
  public function __construct($name = null)
  {
    parent::__construct('album');

    $this->add(array(
        'name' => 'id',
        'type' => 'Hidden',
    ));
    $this->add(array(
        'name' => 'title',
        'options' => array(
            'label' => 'Title',
        ),
        'attributes' => array(
            'type' => 'text',
            'class' => 'form-control',//changed line
        )
    ));
    $this->add(array(
        'name' => 'artist',
        'options' => array(
            'label' => 'Artist',
        ),
        'attributes' => array(
            'type' => 'text',
             'class' => 'form-control',//add form-control class
        )
    ));
    $this->add(array(
        'name' => 'submit',
        'attributes' => array(
            'type' => 'submit',
            'value' => 'Go',
            'id' => 'submitbutton',
             'class' => 'btn btn-default'//add btn class
        ),
    ));
}
}

add.phtml:

    <?php

    $title = 'Add new album';
    $this->headTitle($title);
    ?>

   <h1><?= $this->escapeHtml($title); ?></h1>

   <?php

    $form->setAttribute('action', $this->url('album', array('action' => 'add')));
    $form->prepare();

    echo $this->form()->openTag($form);
    echo $this->formHidden($form->get('id'));
    echo $this->formRow($form->get('title'));
    echo '<br>';//separate line
    echo $this->formRow($form->get('artist'));
    echo '<br>';//separate line
    echo $this->formSubmit($form->get('submit'));
    echo $this->form()->closeTag();

谢谢..

答案 1 :(得分:1)

您必须将表单元素包装到“表单控件”类中。然而,有一个非常好的模块称为zf2 twb bundle,它有一些有用的viewhelpers / form元素等。

答案 2 :(得分:0)

更好地使用bootstrap表单组而不是添加到add.phtml:

<?php

    $title = 'Add new album';
    $this->headTitle($title);
    ?>

   <h1><?= $this->escapeHtml($title); ?></h1>

   <?php

    $form->setAttribute('action', $this->url('album', array('action' => 'add')));
    $form->prepare();

    echo $this->form()->openTag($form);
    echo $this->formHidden($form->get('id'));
    echo $this->formRow($form->get('title'));
    echo '<div class="form-group">';
    echo $this->formRow($form->get('artist'));
    echo '</div><div class="form-group">';
    echo $this->formSubmit($form->get('submit'));
    echo $this->form()->closeTag();