gpt4 book ai didi

php - ZF2 Form Collections - 过滤空记录

转载 作者:可可西里 更新时间:2023-11-01 13:01:22 24 4
gpt4 key购买 nike

我正在研究如何从表单集合中过滤空记录。在我的应用程序中,我有 2 个实体,CompetitionLeague。一场比赛可能有零个或多个联赛。

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

CompetitionFieldset添加到CompetitionForm中,CompetitionFieldset使用Zend\Form\Element\Collection允许用户添加 1 个或多个联赛。我在下面为每个类添加了当前代码。

默认情况下,我想在比赛中显示 3 个联赛的输入字段,因此在 CompetitionFieldset 中,当我添加 Zend\Form\Element\Collection 项时,我将计数选项设置为 3。

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

例如,如果我在 LeagueFieldset 上设置一个 InputFilter 以使 name 字段成为必需字段,那么表单将不会通过验证。

我也许还应该提到我正在使用 Doctrine2 来建模我的实体并补充我的形式等。

我确信我可以在我的模型上使用一些额外的代码,甚至在我处理表单的 Controller 中使用它,但我确信有一个更简洁的解决方案可能使用过滤器。

如果有人能指出我正确的方向,将不胜感激。

非常感谢您提供的任何帮助。

:wq
familymangreg

我的比赛表格

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 过滤器,以捕获提交表单事件并在提交之前删除空字段。

关于php - ZF2 Form Collections - 过滤空记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15832841/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com