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,16 +1462,29 @@ class Form extends ViewableData implements HasRequestHandler
$val = null;
if (is_object($data)) {
$exists = (
isset($data->$name) ||
$data->hasMethod($name) ||
($data->hasMethod('hasField') && $data->hasField($name))
);
// Allow dot-syntax traversal of has-one relations fields
if (strpos($name, '.') !== false && $data->hasMethod('relField')) {
$val = $data->relField($name);
$exists = true;
if ($exists) {
$val = $data->__get($name);
// Regular ViewableData access
} else {
$exists = (
isset($data->$name) ||
$data->hasMethod($name) ||
($data->hasMethod('hasField') && $data->hasField($name))
);
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());
}
}