简体   繁体   中英

Rendering Zend Form in Slim3

I am using Slim3 with Zend Forms 2.6 ( https://packagist.org/packages/zendframework/zend-form ). I am trying to render the form but I get an error:

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

I have added my form as a service within Slim3:

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

Here is my form:

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

I am attempting to render the form within one of my routes (eventually I will pass this to my view, but I'm testing):

$form = $this->QuizForm;

# Doesn't work
print $form;

# Doesn't work
print $form->render()

What's confusing me is the docs say printing the form should work: http://framework.zend.com/manual/1.12/en/zend.form.quickstart.html#zend.form.quickstart.render

I am not using the Zend Framework. I am only using the Form component.

How do I render the form?

You're looking at wrong place. That's documentation for Zend's version 1.12. You are using newest version, so take a look here .

According to that doc resource, before rendering, you should call

$form->prepare();

Also you probably should use Form View Helpers if you want to make rendering forms easy.

It looks like you don't have a proper DI setup, if you can't dump the form like so... php var_dump($this->QuizForm); Then that object is not in the context of this .

I suspect that you might be missing injecting it into the class where you are calling this. But your problem currently is that you need to inject the quiz into your object where you are trying to render it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM