ZF-8022: Form isValid() when using elements that are using belongsTo()

Description

I'm pretty sure that this is a bug, but I may be mistaken.

This issue occurs when building forms with many subforms. Within one of these subforms is at least one element using belongsTo(). The problem is that when the form is submitted and isValid() is applied to the form, the value of the 'arrayed' elements defaults to null even if a value was entered pre-submission.

I have had a look around and I believe that this may be something to do with the problem, but please forgive me as I'm not a great developer. It seems to be that when validating, it can't find the key for the $data array, due to it being nested. In my own code I have modified the isValid() function and now my forms are working as expected, so I hope this helps.

I have modified (around line 2004 of Zend/Form.php):

    foreach ($this->getElements() as $key => $element) {
        $element->setTranslator($translator);
        if (!isset($data[$key])) {
            $valid = $element->isValid(null, $data) && $valid;
        } else {
            $valid = $element->isValid($data[$key], $data) && $valid;
        }
    }

My modification is as follows:

        if (null != $element->getBelongsTo()) {
            $parent_array = $data[$element->getBelongsTo()];
            $key = $parent_array[$key];
            $valid = $element->isValid($key, $data) && $valid;
        } else {

            if (!isset($data[$key])) {
                $valid = $element->isValid(null, $data) && $valid;
            } else {
              $valid = $element->isValid($data[$key], $data) && $valid;
            }
        }

Comments

Still having the same issues.

Partially Fixed in [ZF-9348]

Will work on belongsTo after finishing elementsBelongTo.

Duplicates now parent ZF-9607 which has a Patch and Unit Test attached.