ZF 2.3在不同模块中显示表格

时间:2015-02-01 14:52:49

标签: php forms view module zend-framework2

我尝试使用Zend Framework 2创建一个简单的应用程序,现在堆叠在添加注释表单的位置。
我有两个模块" Book"和"评论"。我想显示一个表单,用于添加在模块中放置的注释" Comment"来自" Books"模块控制器。
当我尝试打开表单标记时出现问题。
我有这个错误:"致命错误:调用未定义的方法Comment \ Form \ CommentForm :: openTag()"
我尝试检查$ this-> addComment - 使用var_dump,一切看起来都很棒。 有人可以帮我解决这个问题吗?

            <div class="row">
                <div class="col-xs-12 commentBlock">
<?php
    if($this->addComment){
        $this->addComment->prepare();
        $commentFieldset = $this->addComment->get('comment');
        print $this->addComment()->openTag($this->addComment);
    }
?>                  
                </div>
            </div>

这是&#39;评论&#39;模块文件: 的注释\表格\ CommentFieldset

<?php

namespace Comment\Form;

use Comment\Entity\Comment;
use Zend\Form\Fieldset;
use Zend\InputFilter\InputFilterProviderInterface;
use Zend\Stdlib\Hydrator\ClassMethods as ClassMethodsHydrator;

class CommentFieldset extends Fieldset implements InputFilterProviderInterface
{
    public function __construct()
    {
        parent::__construct('comment');

        $this->add(
            array(
                'name' => 'commentTitle',
                'attributes' => array(
//                  'required' => 'required',
                    'class' => 'form-controll',
                    'placeholder' => 'Введите заголовок комментария'
                ),
                'options' => array(
                    'label' => 'Заголовок комментария',
                    'label_attributes' => array(
                        'class' => 'text-center col-sm-3 control-label',
                    ),
                ),
            )
        );

        $this->add(
            array(
                'name' => 'commentText',
                'type' => 'Zend\Form\Element\Textarea',
                'attributes' => array(
                    'required' => 'required', 
                    'class' => 'form=-controll',
                    'palceholder' => 'Введите текст комментария',
                ),
                'options' => array(
                    'label' => 'Текст комментария', 
                    'label_attributes' => array(
                        'class' => 'text-center col-sm-3 control-label',
                    ),
                ),
            )
        );

        $this->add(
            array(
                'name' => 'commentType',
                'type' => 'Zend\Form\Element\Radio',
                'attributes' => array(
                    'required' => 'required',
                ),
                'options' => array(
                    'label' => 'Тип комментария',
                    'label_attributes' => array(
                        'class' => 'text-center col-sm-3 control-label',
                    ),
                    'value_options' => array(
                        'positive' => 'postive',
                        'neutral' => 'neutral',
                        'negative' => 'negative',
                    ),
                ),
            )
        );
    }

    public function getInputFilterSpecification()
    {
        return array(
            'commentTitle' => array(
                'required' => FALSE,
                'filters' => array(
                    array(
                        'name' => 'Zend\Filter\StripTags',
                    ),
                    array(
                        'name' => 'Zend\Filter\HtmlEntities'
                    ),
                    array(
                        'name' => 'Zend\Filter\StringTrim',
                    ),
                ),
                'validators' => array(
                    array(
                        'name' => 'Zend\Validator\StringLength',
                        'options' => array(
                            'min' => 1,
                            'max' => 250,
                        ),
                    ),
                ),
            ),
            'commentText' => array(
                'required' => TRUE,
                'filters' => array(
                    array(
                        'name' => 'Zend\Filter\StripTags',
                    ),
                    array(
                        'name' => 'Zend\Filter\HtmlEntities'
                    ),
                    array(
                        'name' => 'Zend\Filter\StringTrim',
                    ),
                ),
                'validators' => array(
                    array(
                        'name' => '\Zend\Validator\StringLength',
                        'options' => array(
                            'min' => 1,
                            'max' => 1000,
                        ),
                    ),
                ),
            ),
            'commentType' => array(
                'required' => true,
                'validators' => array(
                    array(
                        'name' => 'Zend\Validator\InArray', 
                        'options' => array(
                            'haystack' => array(
                                'positive',
                                'neutral',
                                'negative',
                            ),
                            'strict'   =>\Zend\Validator\InArray::COMPARE_STRICT,
                        ),
                    ),
                ),
            ),
        );
    }
}

注释\表格\ CommentFieldset

<?php

namespace Comment\Form;

use Zend\Form\Form;
use Zend\InputFilter\InputFilter;
use Zend\Stdlib\Hydrator\ClassMethods as ClassMethodsHydrator;

class CommentForm extends Form
{
    public function __construct() 
    {
        parent::__construct('comment');

        $this 
            ->setAttribute('method', 'POST')
            ->setHydrator(new ClassMethodsHydrator())
            ->setInputFilter(new InputFilter())
        ;

        $this->add(
            array(
                'type' => 'Comment\Form\CommentFieldset',
                'options' => array(
                    'use_as_base_fieldset' => true,
                ),
            )
        );

        $this->add(
            array(
                'name' => 'csrf', 
                'type' => 'Zend\Form\Element\Csrf',
            )
        );

        $this->add(
            array(
                'name' => 'captcha', 
                'type' => 'Zend\Form\Element\Captcha',
                'options' => array(
                    'label' => 'А вы точно-приточно не робот ? ',
                    'captcha' => new \Zend\Captcha\Figlet(),
                'attributes' => array(
                    'required' => true,
                    )
                ),
            )
        );

        $this->add(
            array(
                'name' => 'submit', 
                'attributes' => array(
                    'type' => 'submit',
                    'value' => 'Добавить комментарий',
                    'class' => 'bigSubmit btn btn-pink', 
                ),
            )
        );

        $this->setValidationGroup(
            array(
                'csrf', 
                'captcha',
                'comment' => array(
                    'commentTitle',
                    'commentText',
                    'commentType',
                ),
            )   
        );
    }

}

评论\ View \ Helper \ DisplayAddCommentForm

<?php

namespace Comment\View\Helper;

use Zend\Form\View\Helper\AbstractHelper;

class DisplayAddCommentForm extends AbstractHelper 
{
    public function __invoke()
    {
        return new \Comment\Form\CommentForm();
    }
}

图书\控制器\ BookController的

<?php

namespace Book\Controller;

use Zend\Mvc\Controller\AbstractActionController;
use Zend\View\Model\ViewModel;
use Zend\View\Model\JsonModel;
use Comment\Form\CommentForm;


class BookController extends AbstractActionController
{
    protected $bookModel;
    protected $commentsModel;

    public function showAction()
    {
        $isbn = $this->params()->fromRoute('num');
        $book = $this->bookModel->getU(array($isbn));
        $comments =  $this->commentsModel->getBookComments(array($isbn));

        if($_SESSION['user']){
            $commentForm = new CommentForm();
        } else {
            $commentForm =  NULL;
        }       
        return new ViewModel(
                array( 
                    'book' => $book,
                    'comments' => $comments,
                    'addComment' => $commentForm,
                )
        );
    }
**************
}

1 个答案:

答案 0 :(得分:1)

openTag()是来自表单视图助手的方法,而不是来自助手的方法。在您的视图脚本中,将此行print $this->addComment()->openTag($this->addComment);更改为此print $this->form()->openTag($this->addComment);