php - ZF2 Form Collections - filtering empty records -
i'm trying work out how filter empty records form collection. application have 2 entities, competition
, league
. competition may have 0 or more leagues.
so create competition form (competitionform
), competition fieldset (competitionfieldset
) , league fieldset (leaguefieldset
).
the competitionfieldset
added competitionform
, , competitionfieldset
uses zend\form\element\collection
allow user add 1 or more leagues. i've added current code each class below.
by default, want display input fields 3 leagues within competition, within competitionfieldset
, when add zend\form\element\collection
item, set count option 3.
but if user doesn't supply data leagues, want ignore them. @ present, 3 empty associated leagues created within database.
if set inputfilter
on leaguefieldset
make name
field required example, form won't validate.
i should maybe mention i'm using doctrine2 model entities , hydrate forms etc.
i'm sure make work additional code on models, or in controller process form, i'm sure there neater solution maybe using filters.
if point me in right direction appreciated.
many in advance can provide.
:wq familymangreg
my competitionform
use kickoff\form\abstractadminform; use doctrine\common\persistence\objectmanager; use doctrinemodule\stdlib\hydrator\doctrineobject 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', ), )); } }
my competitionfieldset
use kickoff\form\abstractfieldset; use kickoff\model\entities\competition; use doctrine\common\persistence\objectmanager; use doctrinemodule\stdlib\hydrator\doctrineobject 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, ), )); } }
my leaguefieldset
use kickoff\form\abstractfieldset; use kickoff\model\entities\league; use doctrine\common\persistence\objectmanager; use doctrinemodule\stdlib\hydrator\doctrineobject 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, ) ); } }
you need data request before pass entitymanager, that's sure. add own validation filter empty records. can provide javascript filters catch submit form event , delete empty fields before submitting it.
Comments
Post a Comment