2016-08-31 5 views
1

私はZF2を学習し、フォームを作成しようとしているが、私は、フォームのアクションを呼び出すURLを実行するたびに、私は次のメッセージを取得しています:以下の場合は私のスタックをZend Frameworkの2のフォームフィールドセットエラー

Zend\Form\Fieldset::add requires that $elementOrFieldset be an object implementing Zend\Form\ElementInterface; received "string" 

を:

#0 /Users/cesar/Documents/zf2course/vendor/zendframework/zend-form/src/Form.php(179):Zendの\フォーム\ Fieldset->( '場所'、アレイ)を追加

#1/Users/cesar/Documents/zf2course/module/Application/src/Application/Form/Info。 php(69):Zend \ Form \ Form-> add( 'location')

#2 /Users/cesar/Documents/zf2course/module/Application/src/Application/Controller/IndexController.php(25): Application \ Form \ Info \ __ construct()

#3 /Users/cesar/Documents/zf2course/vendor/zendframework/zend-mvc/src/Controller/AbstractActionController.php(82):Application \ Controller \ IndexController- > infoAction()

#4 [内部機能]:Zendの\ MVC \コントローラ\ AbstractActionController-> onDispatch(オブジェクト(Zendの\ MVC \ MvcEvent))

#5 /ユーザ/シーザー/ドキュメント/ zf2course /ベンダー/ Zendfram

#6/Users/cesar/Documents/zf2course/vendor/zendframework/zend-eventmanager(zend-MvcEvent)のework/zend-eventmanager/src/EventManager.php(490) /src/EventManager.php(263):Zend \ EventManager \ EventManager-> triggerListeners( 'dispatch'、オブジェクト(Zend \ Mvc \ MvcEvent)、オブジェクト(Closure))

#7/Users/cesar/Documents/(Zend \ Mvc \ MvcEvent))

#8のように表示されます。 /Users/cesar/Documents/zf2course/vendor/zendframework/zend-mvc/src/DispatchListener.php(118):Zend \ Mvc \ Controller \ Abstra

#9 [内部関数]:Zend \ Mvc \ DispatchListener-> onDispatch(Object(Zend \ Http \ PhpEnvironment \ Response) \ MVC \ MvcEvent))

#10 /Users/cesar/Documents/zf2course/vendor/zendframework/zend-eventmanager/src/EventManager.php(490):call_user_func(アレイ、オブジェクト(Zendの\ MVC \ MvcEvent) )

#11 /Users/cesar/Documents/zf2course/vendor/zendframework/zend-eventmanager/src/EventManager.php(263):Zendの\ EventManager \ EventManager-> triggerListeners( '派遣'、オブジェクト(Zendの\ Mvc \ MvcEvent)、オブジェクト(Closure)

Zend \ EventManager \ EventManager-> triggerEventUntil(Object(Closure)、Object(Zend \ Mvc \))、およびZend \ MendFactory(Zend \ Mvc \

#13 /ユーザー/ Cesar/Documents/zf2course/public/index。PHP(21):Zendの\ MVC \応用 - >ファイル名を指定して実行()

#{メイン} 14は

は、私はこのようなフォームクラスを作成します。

<?php 
namespace Application\Form; 

use Zend\Form\Form; 
use Zend\Form\Element; 

class Info extends Form 
{ 
    public function __construct() 
    { 
     parent::__construct('info'); 

     $location = new Element('location'); 
     $location->setLabel('Location'); 
     $location->setAttribute(array(
      'type' => 'text', 
      'class' => 'form-control', 
    )); 

     $sizeW = new Element\Number('size_w'); 
     $sizeW->setLabel('Width Size'); 
     $sizeW->setAttributes(array(
       'min' => '0', 
       'max' => '500', 
       'step' => '0.1', 
       'class' => 'form-control' 
      )); 

     $sizeH = new Element\Number('size_h'); 
     $sizeH->setLabel('Height Size'); 
     $sizeH->setAttributes(array(
       'min' => '0', 
       'max' => '500', 
       'step' => '0.1', 
       'class' => 'form-control' 
      ));  

     $type = new Element\Select('plot_type'); 
     $type->setLabel('Plot Type'); 
     $type->setAttribute('class', 'form-control'); 
     $type->setValueOptions(array(
      1 => 'Balcony', 
      2 => 'Plot', 
    )); 

     $family = new Element\Number('family'); 
     $family->setLabel('Family Aggregate Number'); 
     $family->setAttributes(array(
       'min' => '0', 
       'max' => '10', 
       'step' => '1', 
       'class' => 'form-control' 
    )); 

     $diff = new Element\Select('diff'); 
     $diff->setLabel('Gardening Type'); 
     $diff->setAttribute('class', 'form-control'); 
     $diff->setValueOptions(array(
      1 => 'Begginner', 
      2 => 'Advanced', 
    )); 

     $submit = new Element\Submit('submit'); 
     $submit->setValue('Submit'); 
     $submit->setAttribute('class', 'btn btn-primary'); 

     $this->add('location'); 
     $this->add('size_w'); 
     $this->add('size_h'); 
     $this->add('plot_type'); 
     $this->add('family'); 
     $this->add('diff'); 
     $this->add('submit'); 
    } 
} 

そして私はinfocontrollerと呼ばれますそのようなフォームで:

... 
    public function infoAction() 
    { 
     $form = new Info(); 

     if ($this->request->isPost()) 
     { 
      $form->setData($this->request->getPost()); 
      // save stuff 

     } 

     return new ViewModel(array(
      'form' => $form, 
     )); 
    } 

私は何かが足りないか、私は、このフィールドセットクラスを作成するために必要でしたし、それは私にこのエラーを与えている理由ですか? また誰かが私に送る良いzf2チュートリアルを持っているなら、それはとてもいいと思います。

答えて

1

なぜ文字列パラメータを持つこれらの行:

$this->add('location'); 
    $this->add('size_w'); 
    $this->add('size_h'); 
    $this->add('plot_type'); 
    $this->add('family'); 
    $this->add('diff'); 
    $this->add('submit'); 

あなたは直前の変数内のすべての要素を定義すると?これらの変数によってそれらを交換するようにしてください:ZF2チュートリアルに関するご質問については

$this->add($location); 
    $this->add($sizeW); 
    $this->add($sizeH); 
    $this->add($type); 
    $this->add($family); 
    $this->add($diff); 
    $this->add($submit); 

offical oneは良いものです、あなたはそれを試してみましたか?

+0

私はチュートリアルを間違って読んでいました。 私はそのチュートリアルにも従っていましたが、ちょっと混乱しました。 多分、ZFは私にとって現時点では先進的ですが、私は努力しています。 これはトリックを行い、フォームを機能させることができました。ありがとう –

関連する問題