You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
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:I've updated it to the following:
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.
Originally posted by @rkeet at zendframework/zend-form#173
The text was updated successfully, but these errors were encountered: