2013-04-05 77 views
6

我正在尝试如何筛选表单集合中的空记录。通过我的申请,我有两个实体,CompetitionLeague。一场比赛可能有零或更多的联赛。所以我创建了一个竞赛表格(CompetitionForm),一个竞赛字段集合(CompetitionFieldset)和一个联盟字段集合(LeagueFieldset)。ZF2表单集合 - 筛选空记录

CompetitionFieldset添加到CompetitionFormCompetitionFieldset使用Zend\Form\Element\Collection允许用户添加1个或更多联赛。我为下面的每个课程添加了当前的代码。

默认情况下,我想在比赛中显示了3轮联赛的输入字段,所以CompetitionFieldset内,当我添加了Zend\Form\Element\Collection项目,我计数选项设置为3

但是,如果用户没有按”为联赛提供任何数据,我想忽略他们。目前,我的数据库中创建了三个空的关联联赛。

如果我在LeagueFieldset上设置了InputFilter以使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运行该检查之前迭代联盟条目。如果它是空的,不要保存它。也许不是最有效的地方,因为空白字段集仍然会有验证器和过滤器运行,但它可以工作。 – 2014-05-02 20:27:01

+0

你能否在你的控制器中发布相关的代码,并在你的教义实体中发布关联? – YRM 2014-05-09 09:37:31

回答

0

你需要将它传递给EntityManager的之前从请求得到的数据,即”当然。只需添加您自己的验证即可过滤空记录。您也可以提供JavaScript过滤器,以提交表单事件并在提交之前删除空字段。