【问题标题】:ZF 2.3 displaying form in different modulesZF 2.3 不同模块的显示形式
【发布时间】:2015-02-01 14:52:49
【问题描述】:

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

            <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>

这里是“评论”模块文件: Comment\Form\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,
                        ),
                    ),
                ),
            ),
        );
    }
}

Comment\Form\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',
                ),
            )   
        );
    }

}

Comment\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();
    }
}

书\控制器\书控制器

<?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,
                )
        );
    }
**************
}

【问题讨论】:

    标签: php forms view module zend-framework2


    【解决方案1】:

    openTag() 是来自表单视图助手的方法,而不是来自您的助手。在您的视图脚本中更改这一行:print $this-&gt;addComment()-&gt;openTag($this-&gt;addComment); 到这一行 print $this-&gt;form()-&gt;openTag($this-&gt;addComment);

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-10-08
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多