FIX Handle fields with square brackets

This commit is contained in:
Daniel Hensby 2016-12-08 16:46:15 +00:00
parent 7ca876a4c7
commit 5248be9226
No known key found for this signature in database
GPG Key ID: B00D1E9767F0B06E

View File

@ -1418,6 +1418,7 @@ class Form extends RequestHandler {
if(is_object($data)) $this->record = $data; if(is_object($data)) $this->record = $data;
// dont include fields without data // dont include fields without data
/** @var FormField[] $dataFields */
$dataFields = $this->Fields()->dataFields(); $dataFields = $this->Fields()->dataFields();
if($dataFields) foreach($dataFields as $field) { if($dataFields) foreach($dataFields as $field) {
$name = $field->getName(); $name = $field->getName();
@ -1450,15 +1451,31 @@ class Form extends RequestHandler {
$val = $data[$name]; $val = $data[$name];
} }
// If field is in array-notation we need to access nested data // If field is in array-notation we need to access nested data
else if(strpos($name,'[')) { else if(preg_match_all('/(.*)\[(.*)\]/U', $name, $matches)) {
// First encode data using PHP's method of converting nested arrays to form data //discard first match which is just the whole string
$flatData = urldecode(http_build_query($data)); array_shift($matches);
// Then pull the value out from that flattened string
preg_match('/' . addcslashes($name,'[]') . '=([^&]*)/', $flatData, $matches);
if (isset($matches[1])) { $keys = array_pop($matches);
$exists = true; $name = array_shift($matches);
$val = $matches[1]; $name = array_shift($name);
if (array_key_exists($name, $data)) {
$tmpData = &$data[$name];
// drill down into the data array looking for the corresponding value
foreach ($keys as $arrayKey) {
if ($arrayKey !== '') {
$tmpData = &$tmpData[$arrayKey];
} else {
//empty square brackets means new array
if (is_array($tmpData)) {
$tmpData = array_shift($tmpData);
}
}
}
if ($tmpData) {
$val = $tmpData;
$exists = true;
}
} }
} }
} }