mirror of
https://github.com/silverstripe/silverstripe-framework
synced 2024-10-22 14:05:37 +02:00
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:
parent
713a0b4a67
commit
02fb7c3b17
@ -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];
|
||||
|
@ -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());
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user