我正在尝试解决如何从表单集合中过滤空记录。在我的应用程序中,我有 2 个实体,Competition
并且League
. 一场比赛可能有零个或多个联赛。
所以我创建了一个竞赛表单 ( CompetitionForm
)、一个竞赛字段集 ( CompetitionFieldset
) 和一个联赛字段集 ( LeagueFieldset
)。
被CompetitionFieldset
添加到CompetitionForm
, 和用于允许用户添加 1 个或多个联赛的CompetitionFieldset
用途Zend\Form\Element\Collection
。我为下面的每个类添加了当前代码。
默认情况下,我想在比赛中显示 3 个联赛的输入字段,因此在CompetitionFieldset
添加Zend\Form\Element\Collection
项目时,我将计数选项设置为 3。
但是如果用户没有为联赛提供任何数据,我想忽略它们。目前,在我的数据库中创建了三个空的关联联赛。
例如,如果我设置一个InputFilter
以LeagueFieldset
使该name
字段成为必填字段,则表单将无法验证。
我也许还应该提到我正在使用 Doctrine2 来建模我的实体并为我的表单补充水分等。
我确信我可以在我的模型上使用一些额外的代码,甚至在我处理表单的控制器中使用它,但我确信有一个更简洁的解决方案可能使用过滤器。
如果有人能指出我正确的方向,将不胜感激。
非常感谢您提供的任何帮助。
: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',
),
));
}
}
我的比赛场集
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,
),
));
}
}
我的联赛场集
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,
)
);
}
}