fam*_*reg 7 php zend-form doctrine-orm zend-framework2
我正在尝试研究如何从表单集合中过滤空记录.在我的应用程序中,我有2个实体,Competition和League.比赛可能有零个或多个联赛.
所以我创建了一个竞赛表单(CompetitionForm),一个竞赛字段集(CompetitionFieldset)和一个联盟字段集(LeagueFieldset).
将CompetitionFieldset被添加到CompetitionForm和CompetitionFieldset用途Zend\Form\Element\Collection,以允许用户添加1个或多个联盟.我已经为下面的每个类添加了当前代码.
默认情况下,我想在比赛中显示3个联赛的输入字段,因此在CompetitionFieldset我添加Zend\Form\Element\Collection项目时,我将计数选项设置为3.
但如果用户没有为联盟提供任何数据,我想忽略它们.目前,我的数据库中创建了三个空的关联联盟.
如果我设置InputFilter上LeagueFieldset,使name例如必填字段,则表单不会验证.
我也许应该提一下,我正在使用Doctrine2来模拟我的实体并为我的表格补水等.
我敢肯定,我可以把它与一些额外的代码在我的模型在我的控制,我处理的形式工作,甚至,但我敢肯定,有可能使用的过滤器更为整洁的解决方案.
如果有人能指出我正确的方向,我将不胜感激.
非常感谢您提供的任何帮助.
:wq
familymangreg
Run Code Online (Sandbox Code Playgroud)
我的竞赛形式
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',
),
));
}
}
Run Code Online (Sandbox Code Playgroud)
我的竞赛场地
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,
),
));
}
}
Run Code Online (Sandbox Code Playgroud)
我的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,
)
);
}
}
Run Code Online (Sandbox Code Playgroud)
在将请求传递给 EntityManager 之前,您需要从请求中获取数据,这是肯定的。只需添加您自己的验证即可过滤空记录。您还可以提供 javascript 过滤器,该过滤器将捕获提交表单事件并在提交之前删除空字段。
| 归档时间: |
|
| 查看次数: |
1164 次 |
| 最近记录: |