Skip to content
New issue

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

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
weierophinney opened this issue Dec 31, 2019 · 0 comments

Comments

@weierophinney
Copy link
Member

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


Originally posted by @rkeet at zendframework/zend-form#173

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

No branches or pull requests

1 participant