Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

Already on GitHub? Sign in to your account

Collection dealing with objects that can not be overwritten problem. #2575

zfbot opened this Issue Sep 28, 2012 · 3 comments


None yet
3 participants

zfbot commented Sep 28, 2012

Jira Information

Original Issue:ZF2-545
Issue Type:Improvement
Reporter:Oliver Leisalu


We have a case where we need to use Collection, but elements in collection can not be overwritten.
For example Invoice and InvoiceRow.
InvoiceRow has property "IsDeleted", so if we remove a row we have to set IsDeleted to 1.

Now problems start if we allow removing and adding new elements at the same time.

We did these changes to Form and Collection class:
prepareValidationGroup method had for loop that used $i property values 0,1,2,3... etc
But if some element was removed, for example element 1, then it should not add validation group for fieldset named "1".

Fix is very simple, add foreach loop that uses input data instead of for loop

//for ($i = 0 ; $i != $count ; ++$i) {
foreach( $data[$key] as $i => $x ) {

This way validation works correctly.

Furthermore some more changes were required to allow removing elements correctly. For that we changed {{Collection::populateValues}} method.
We changed the part that deals with removing elements to this. Now elements are added/removed according to key, not by just count.
(Because if we remove one entity and add new, count is the same, but objects are not and in DB we can't just update old object with new values. We had to set "IsDeleted=1" to them and always add new rows as separate rows)

foreach ($this->byName as $name => $elementOrFieldset) {
if (isset($data[$name])) { continue; }

if (!$this->allowRemove) { throw new Exception\DomainException(sprintf( 'There are fewer elements than specified in the collection (%s). Either set the allow_remove option ' . 'to true, or re-submit the form.', get_class($this) ) ); }


I believe this functionality should be provided out of the box, as the case explained here is quite common.
One solution was to make a copy of Collection, but that was not possible because of "special behaviour" for collections in Form prepareValidationGroup method.

zfbot commented Sep 28, 2012

(Originally posted by: froschdesign on 09/11/12)

Code tags added.

zfbot commented Sep 28, 2012

This issue was ported from the ZF2 Jira Issue Tracker at

Known GitHub users mentioned in the original message or comment:
@weierophinney, @froschdesign

@ghost ghost assigned weierophinney Sep 28, 2012

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment