I’ve got a problem with my collections. I’ve been following the example tutorial (https://github.com/doctrine/DoctrineModule/blob/master/docs/hydrator.md#a-complete-example-using-zendform) and have succesfully made a Blogpost
and Tag
entity. In my controller I’ve an add action and edit action. My add and edit views contain the following code:
<?php
echo $this->formElement($blogpost->get('addTag'));
$tag = $blogpost->get('tags');
?>
<fieldset id="tagFieldset">
<?php
echo $this->formCollection($tag);
?>
</fieldset>
The view has a button for adding tags dynamically (like in this example: http://zf2.readthedocs.org/en/latest/modules/zend.form.collections.html#adding-new-elements-dynamically), which works fine in the edit-action. When adding a new Blogpost
with 2 tags at the same time, it only adds 1 Tag
in the database (the count option is set to 1. When I increase this number, it is able to add tags according to the option). When I edit that Blogpost
and add 2 tags (total of three), 2 tags are added to the database.
When debugging the addTags() function, the parameter holds only 1 Tag object at the add-action. In the edit-action it holds all the new tags that have been added (in my case, 2 new tags).
BlogpostFieldset look likes:
$this->add(array(
'name' => 'addTag',
'type' => 'button',
'options' => array(
'label' => 'Add more tags',
),
'attributes' => array(
'id' => 'addTag'
)
));
$tagFieldset = new TagFieldset($serviceManager);
$this->add(array(
'type' => 'Zend\Form\Element\Collection',
'name' => 'tags',
'options' => array(
'count' => 1,
'target_element' => $tagFieldset,
'should_create_template' => true,
'allow_add' => true,
)
));
Javascript for adding fieldsets:
$('#addTag').click(function(e) {
var currentCount = $('form > #tagFieldset > label').length;
alert(currentCount);
//Set datatemplate (generated by the arraycollection)
var template = $('form > #tagFieldset > span').data('template');
//Replace the template to other addressfieldset
template = template.replace(/__index__/g, currentCount);
//Set new fieldset
$('form > #tagFieldset').append(template);
});
It renders my templates correctly. Works fine in my edit-action, but my add-action only adds 1 tag. The problem exists in my controller by adding inputfilters or validators. In the case of adding a new tag I want to have some specific filters/validators.
The following code has been used to add the filter(s)/validators to the form/fieldset they have to been put on:
public function addAction()
{
$form = new Form($this->serviceLocator);
$entity= new Entity();
$form->bind($entity);
if ($this->request->isPost()) {
$data = $this->request->getPost();
$form->getInputFilter()->get('fieldset')->get('input')->setRequired(true);
$form->getInputFilter()->get('fieldset')->get('input')->allowEmpty(false);
$form->setData($data);
if ($form->isValid()) {
// Handle the rest off this action.
}
}
return array(
'form' => $form
)
}
When removing the following lines from the controller - addAction()
:
$form->getInputFilter()->get('fieldset')->get('input')->setRequired(true);
$form->getInputFilter()->get('fieldset')->get('input')->allowEmpty(false);
It is able to add more Tags
than the given option that has been set at the count
of the tagFieldset
that I've created.
Those inputfilters that I set in the addAction()
are NOT on the collection fieldset. By addepting (with new inputfilters) my basefieldset, the collection fieldset is not able to add any more tags than the given count
option. In this case it is useless to add anymore tagfieldsets with the Javascript function, since they will not be added.