Skip to content
This repository has been archived by the owner on Jan 30, 2020. It is now read-only.

function prepareBindData creates data in return array when nested fieldset empty #173

Open
rkeet opened this issue Jul 31, 2017 · 1 comment

Comments

@rkeet
Copy link
Contributor

rkeet commented Jul 31, 2017

The prepareBindData(...) {...} function creates data in the resulting $data array when a fieldset is prepared, which as a child fieldset.

I used Doctrine, so maybe it was the combination, but it had me stumped for quite a while.

Function in Form.php is as follows:

protected function prepareBindData(array $values, array $match)
{
    $data = [];
    foreach ($values as $name => $value) {
        if (! array_key_exists($name, $match)) {
            continue;
        }
        if (is_array($value) && is_array($match[$name])) {
            $data[$name] = $this->prepareBindData($value, $match[$name]);
        } else {
            $data[$name] = $value;
        }
    }
    return $data;
}

I've updated it to the following:

protected function prepareBindData(array $values, array $match)
{
    $data = [];
    foreach ($values as $name => $value) {
        if (! array_key_exists($name, $match)) {
            continue;
        }

        if (is_array($value) && is_array($match[$name])) {
            if (!empty(array_filter($value))) {
                $data[$name] = $this->prepareBindData($value, $match[$name]);
            }
        } else {
            $data[$name] = $value;
        }
    }
    return $data;
}

Adding the if (!empty(array_filter($value)) { ... } check ensures that the key is not added to the $data array when the $value is completely empty. As it stands, this can happen, and in my situation it did happen. The reason for it to happen is that $value, an array, contains at least 1 other array. This array in turn may not contain any values, but may contain keys. If it has keys, the first array (parent) is added because it's seen as not empty, though it has no values.

In combination with Doctrine 2 it the tries to create a child element, which in my case resulted in database restriction errors, which is never pretty.

P.s. - First bug report. Not sure how to update this to a PR or something similar. Definitely no idea on how to create test cases for this.

P.p.s. - As test data I've included a screenshot of received data. It's from a "Product" form. A Product may have 0 or more "ProductDetail" Entities (so it's a Collection), 0 or more "Price" Entities and may or may not have "Stock". In my situation it always attempted to create a "Stock" Entity. The proposed code change fixes the issue when the form gets validated after receiving the data in the image below.

image

@weierophinney
Copy link
Member

This repository has been closed and moved to laminas/laminas-form; a new issue has been opened at laminas/laminas-form#13.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants