渲染Slim3中的Zend窗体


Rendering Zend Form in Slim3

我正在将Slim3与Zend Forms 2.6一起使用(https://packagist.org/packages/zendframework/zend-form)。我正在尝试呈现表单,但我遇到了一个错误:

Fatal error: Call to undefined method QuizApp'Forms'QuizForm::render() in /var/www/QuizApp/Routes/SurveyRoutes.php on line 25

我已经在Slim3:中添加了我的表格作为服务

$container['QuizForm'] = function() use ($app) {
    return new 'QuizApp'Forms'QuizForm($app->SurveyServices);
};

这是我的表格:

class QuizForm extends 'Zend'Form'Form
{ 
    private $survey_services;
    public function __construct(SurveyServices $survey_services, $name = null)
    {
        $this->survey_services = $survey_services;
        parent::__construct($name);
    }
    public function init()
    {
        /*
         * Set form method to POST
         */
        $this->setMethod('POST');
        /*
         * Add submit button to the form
         */   
        $this->add(array(
            'name' => 'submit',
            'type' => 'Submit',
            'attributes' => array(
                'value' => 'Get Quiz Results'
            ),
        ));
    }
}

我正试图在我的一条路线内呈现表单(最终我会将其传递给我的视图,但我正在测试):

$form = $this->QuizForm;
# Doesn't work
print $form;
# Doesn't work
print $form->render()

让我困惑的是,医生们说打印表格应该可以:http://framework.zend.com/manual/1.12/en/zend.form.quickstart.html#zend.form.quickstart.render

我没有使用Zend框架。我只使用Form组件。

如何呈现表单?

你看错地方了。这是Zend 1.12版本的文档。您正在使用最新版本,请查看此处。

根据该文档资源,在渲染之前,您应该调用

$form->prepare();

此外,如果您想使呈现表单变得简单,您可能应该使用"表单视图辅助对象"。

如果不能像这样转储表单,看起来就没有正确的DI设置。。。php var_dump($this->QuizForm); 则该对象不在this的上下文中。

我怀疑你可能错过了将它注入到你调用它的类中。但你目前的问题是,你需要将测试注入到你试图渲染的对象中