ZF2表单集合——过滤空记录


ZF2 Form Collections - filtering empty records

我正试图找出如何从表单集合过滤空记录。在我的应用程序中,我有两个实体,CompetitionLeague。一场比赛可以有0个或多个联赛。

所以我创建了一个竞赛表单(CompetitionForm),一个竞赛字段集(CompetitionFieldset)和一个联赛字段集(LeagueFieldset)。

CompetitionFieldset添加到CompetitionForm中,CompetitionFieldset使用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,
            )
        );
    }
}

在将请求传递给EntityManager之前需要从请求中获取数据,这是肯定的。只需添加您自己的验证来过滤空记录。你也可以提供javascript过滤器来捕捉提交表单事件,并在提交之前删除空字段。