php - ZF2 Form Collections - filtering empty records

848

I'm trying to work out how to filter empty records from a form collection. With my application I have 2 entities,Competition andLeague. A competition may have zero or more Leagues.

So I create a Competition form (CompetitionForm), a Competition fieldset (CompetitionFieldset) and a League fieldset (LeagueFieldset).

TheCompetitionFieldset is added to theCompetitionForm, and theCompetitionFieldset usesZend\Form\Element\Collection to allow the user to add 1 or more Leagues. I've added the current code for each class below.

By default, I want to display input fields for 3 leagues within a competition, so within theCompetitionFieldset, when I add theZend\Form\Element\Collection item, I set the count option to 3.

But if a user doesn't supply any data for the leagues, I want to ignore them. At present, three empty associated leagues are created within my database.

If I set anInputFilter on theLeagueFieldset to make thename field required for example, then the form won't validate.

I should maybe also mention that I'm using Doctrine2 to model my entities and hydrate my forms etc.

I'm sure I could make it 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 neater solution maybe using Filters.

If anyone could point me in the right direction it would be much appreciated.

Many thanks in advance 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 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,
            ),
        ));
    }
}

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 (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,
            )
        );
    }

}
728

Answer

Solution:

You need to get data from request before you pass it to EntityManager, that's for sure. Just add your own validation to filter empty records. You can also provide javascript filters that will catch submit form event and delete empty fields before submitting it.

People are also looking for solutions to the problem: php - How to access form element xyz[] in Javascript?

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.