2013-04-05 9 views
6

フォームコレクションから空のレコードをフィルタリングする方法を検討しようとしています。私のアプリケーションでは、2つのエンティティ、CompetitionLeagueがあります。競技会には、0個以上のリーグがある場合があります。ZF2フォームコレクション - 空のレコードをフィルタリングする

したがって、競技用フォーム(CompetitionForm)、競技用フィールドセット(CompetitionFieldset)、リーグのフィールドセット(LeagueFieldset)を作成します。

CompetitionFieldset

CompetitionFormに添加し、そしてCompetitionFieldsetは、ユーザが1つの以上のリーグを追加できるようにするZend\Form\Element\Collectionを使用しています。下の各クラスの現在のコードを追加しました。デフォルトでは

、私はZend\Form\Element\Collection項目を追加するとき、私は3

にcountオプションを設定し、CompetitionFieldset内ので、競争の中に3つのリーグの入力フィールドを表示したい。しかし、ユーザーは「doesnの場合リーグのデータを提供していないので、無視したい。現在、3つの空の関連リーグがデータベース内に作成されています。

LeagueFieldsetInputFilterを設定して、たとえばnameフィールドを必要とする場合、フォームは検証されません。

私は多分も私はそれが私のモデルにいくつかの追加のコードで動作させることができ確信している、あるいは私の中で私はなど私のエンティティをモデル化し、自分のフォームを水和する

をDoctrine2を使用していることを言及する必要があります私がフォームを処理するコントローラーですが、フィルターを使用しているとは思えません。

誰かが正しい方向に私を指すことができれば、それは非常に感謝します。

ご提供いただけるご支援に感謝します。

:wq 
familymangreg 

マイCompetitionForm

use Kickoff\Form\AbstractAdminForm; 
use Doctrine\Common\Persistence\ObjectManager; 
use DoctrineModule\Stdlib\Hydrator\DoctrineObject as DoctrineHydrator; 
use Zend\Stdlib\Hydrator\ClassMethods; 

class CompetitionForm extends AbstractAdminForm 
{ 
    public function __construct(ObjectManager $objectManager) 
    { 
     parent::__construct($objectManager, 'competition-form'); 

     $fieldset = new CompetitionFieldset($objectManager); 
     $fieldset->setUseAsBaseFieldset(true); 
     $this->add($fieldset); 

     $this->add(array(
      'name' => 'submit', 
      'attributes' => array(
       'type' => 'submit', 
       'value' => 'Submit', 
       'id' => 'submitbutton', 
      ), 
     )); 
    } 
} 

マイCompetitionFieldset

use Kickoff\Form\AbstractFieldset; 
use Kickoff\Model\Entities\Competition; 
use Doctrine\Common\Persistence\ObjectManager; 
use DoctrineModule\Stdlib\Hydrator\DoctrineObject as DoctrineHydrator; 

class CompetitionFieldset extends AbstractFieldset 
{ 
    public function __construct(ObjectManager $objectManager) 
    { 
     parent::__construct($objectManager,'Competition'); 

     $this->setHydrator(new DoctrineHydrator($this->objectManager,'Kickoff\Model\Entities\Competition')) 
      ->setObject(new Competition()); 

     $this->setLabel('Competition'); 

     $this->add(array(
      'name' => 'name', 
      'options' => array(
       'label' => 'Competition name (e.g. Premier League)',     
      ), 
      'attirbutes' => array(
       'type' => 'text', 
      ), 
     )); 

     $this->add(array(
      'name' => 'long_name', 
      'options' => array(
       'label' => 'Competition long name (e.g. Barclays Premier League)', 
      ), 
      'attirbutes' => array(
       'type' => 'text', 
      ), 
     )); 

     $leagueFieldset = new LeagueFieldset($objectManager); 
     $this->add(array(
      'type' => 'Zend\Form\Element\Collection', 
      'name' => 'leagues', 
      'options' => array(
       'label' => 'Leagues', 
       'count' => 3, 
       'should_create_template' => true, 
       'allow_add' => true, 
       'target_element' => $leagueFieldset, 
      ), 
     )); 
    } 
} 

マイLeagueFieldset

use Kickoff\Form\AbstractFieldset; 
use Kickoff\Model\Entities\League; 
use Doctrine\Common\Persistence\ObjectManager; 
use DoctrineModule\Stdlib\Hydrator\DoctrineObject as DoctrineHydrator; 
use Zend\InputFilter\InputFilterProviderInterface; 

class LeagueFieldset extends AbstractFieldset implements InputFilterProviderInterface 
{ 
    public function __construct(ObjectManager $objectManager) 
    { 
     parent::__construct($objectManager,'League'); 

     $this->setHydrator(new DoctrineHydrator($this->objectManager,'Kickoff\Model\Entities\League')) 
      ->setObject(new League()); 

     $this->setLabel('League'); 

     $this->add(array(
      'name' => 'name', 
      'options' => array(
       'label' => 'League name (e.g. First Qualifying Round)', 
      ), 
      'attirbutes' => array(
       'type' => 'text', 
      ), 
     )); 

     $this->add(array(
      'name' => 'long_name', 
      'options' => array(
       'label' => 'League long name (e.g. UEFA Champions League First Qualifying Round)', 
      ), 
      'attirbutes' => array(
       'type' => 'text', 
      ), 
     )); 
    } 

    public function getInputFilterSpecification() 
    { 
     return array(
      'name' => array(
       'required' => true, 
      ) 
     ); 
    } 

} 
+0

フィールドが空であるかどうかをチェックし、そのチェックを実行しているDBにサブミットする直前にリーグの項目を反復するメソッドをリーグモデルに書くことができます。空の場合は保存しないでください。空のフィールドセットには引き続きバリデータがあり、フィルタが実行されているので、チェックを入れるのに最も効率的な場所ではないかもしれませんが、うまくいくでしょう。 –

+0

コントローラに関連するコードとあなたの教義団体の関連付けを投稿することはできますか? – YRM

答えて

0

あなたは「ことを、あなたはEntityManagerのにそれを渡す前に、リクエストからデータを取得する必要があります確かに。空のレコードをフィルタリングするために独自の検証を追加するだけです。送信フォームイベントをキャッチし、空のフィールドを削除するjavascriptフィルタを提出することもできます。

関連する問題