NEW: Support dot syntax in form field names

This change adds support for these in a few places.

 - Form::saveInto($record)
 - Form::loadDataForm($record)
 - Form::loadDataForm($_POST)

Fixes https://github.com/silverstripe/silverstripe-framework/issues/9163
This commit is contained in:
Sam Minnee 2019-08-24 16:28:09 +12:00 committed by Ingo Schommer
parent 713a0b4a67
commit 02fb7c3b17
2 changed files with 32 additions and 9 deletions

View File

@ -1462,6 +1462,13 @@ class Form extends ViewableData implements HasRequestHandler
$val = null;
if (is_object($data)) {
// Allow dot-syntax traversal of has-one relations fields
if (strpos($name, '.') !== false && $data->hasMethod('relField')) {
$val = $data->relField($name);
$exists = true;
// Regular ViewableData access
} else {
$exists = (
isset($data->$name) ||
$data->hasMethod($name) ||
@ -1471,7 +1478,13 @@ class Form extends ViewableData implements HasRequestHandler
if ($exists) {
$val = $data->__get($name);
}
}
// Regular array access. Note that dot-syntax not supported here
} elseif (is_array($data)) {
// PHP turns the '.'s in POST vars into '_'s
$name = str_replace('.', '_', $name);
if (array_key_exists($name, $data)) {
$exists = true;
$val = $data[$name];

View File

@ -469,8 +469,18 @@ class FormField extends RequestHandler
*/
public function saveInto(DataObjectInterface $record)
{
if ($this->name) {
$record->setCastedField($this->name, $this->dataValue());
$component = $record;
$fieldName = $this->name;
// Allow for dot syntax
if (($pos = strrpos($this->name, '.')) !== false) {
$relation = substr($this->name, 0, $pos);
$fieldName = substr($this->name, $pos + 1);
$component = $record->relObject($relation);
}
if ($fieldName) {
$component->setCastedField($fieldName, $this->dataValue());
}
}