I am trying to decide how to filter blank entries from a collection of forms. With my application, I have 2 objects, Competition and League . A competition may have zero or more leagues.
So, I am creating a competition form ( CompetitionForm ), a competition field ( CompetitionFieldset ) and a set of League fields ( LeagueFieldset ).
CompetitionForm added to CompetitionFieldset , and CompetitionFieldset uses Zend\Form\Element\Collection to allow the user to add 1 or more Leagues. I have added the current code for each class below.
By default, I want to display input fields for 3 leagues as part of the competition, so in the CompetitionFieldset , when I add the Zend\Form\Element\Collection element, I set the count parameter to 3.
But if the user does not provide any data for the leagues, I want to ignore them. There are currently three empty associated leagues being created in my database.
If I set the InputFilter to LeagueFieldset to make the name field mandatory, for example, the form will not be validated.
I should also mention that I use Doctrine2 to model my objects and hydrate my forms, etc.
I'm sure I can get it to work with some additional code on my models or even in my controller, where I process the form, but I'm sure there is a more accurate solution, possibly using filters.
If someone can point me in the right direction, it will be very appreciated.
Thanks so much for any help you can provide.
:wq familymangreg
My 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', ), )); } }
My competition fieldset
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 (eg Premier League)', ), 'attirbutes' => array( 'type' => 'text', ), )); $this->add(array( 'name' => 'long_name', 'options' => array( 'label' => 'Competition long name (eg 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 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 (eg First Qualifying Round)', ), 'attirbutes' => array( 'type' => 'text', ), )); $this->add(array( 'name' => 'long_name', 'options' => array( 'label' => 'League long name (eg UEFA Champions League First Qualifying Round)', ), 'attirbutes' => array( 'type' => 'text', ), )); } public function getInputFilterSpecification() { return array( 'name' => array( 'required' => true, ) ); } }