2015-11-23 128 views
2

我使用Slim3和Zend窗體2.6(https://packagist.org/packages/zendframework/zend-form)。我想呈現的形式,但我得到一個錯誤:Slim3渲染Zend窗體

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框架。我只使用表單組件。

如何渲染表單?

回答

1

你正在看錯了地方。這是Zend版本1.12的文檔。 您正在使用最新版本,趕快去看看here

根據該文檔資源,渲染之前,你應該叫

$form->prepare(); 

而且你應該使用Form View Helpers如果你想呈現的形式方便。

0

它看起來像你沒有一個適當的DI設置,如果你不能傾倒的形式是這樣的... php var_dump($this->QuizForm); 則該對象是不是在this上下文。

我懷疑你可能會缺少注入到你調用它的類中。但目前你的問題是,你需要將測驗注入你想要呈現它的對象中。