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,6 +1462,13 @@ class Form extends ViewableData implements HasRequestHandler
|
|||||||
$val = null;
|
$val = null;
|
||||||
|
|
||||||
if (is_object($data)) {
|
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 = (
|
$exists = (
|
||||||
isset($data->$name) ||
|
isset($data->$name) ||
|
||||||
$data->hasMethod($name) ||
|
$data->hasMethod($name) ||
|
||||||
@ -1471,7 +1478,13 @@ class Form extends ViewableData implements HasRequestHandler
|
|||||||
if ($exists) {
|
if ($exists) {
|
||||||
$val = $data->__get($name);
|
$val = $data->__get($name);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Regular array access. Note that dot-syntax not supported here
|
||||||
} elseif (is_array($data)) {
|
} elseif (is_array($data)) {
|
||||||
|
// PHP turns the '.'s in POST vars into '_'s
|
||||||
|
$name = str_replace('.', '_', $name);
|
||||||
|
|
||||||
if (array_key_exists($name, $data)) {
|
if (array_key_exists($name, $data)) {
|
||||||
$exists = true;
|
$exists = true;
|
||||||
$val = $data[$name];
|
$val = $data[$name];
|
||||||
|
@ -469,8 +469,18 @@ class FormField extends RequestHandler
|
|||||||
*/
|
*/
|
||||||
public function saveInto(DataObjectInterface $record)
|
public function saveInto(DataObjectInterface $record)
|
||||||
{
|
{
|
||||||
if ($this->name) {
|
$component = $record;
|
||||||
$record->setCastedField($this->name, $this->dataValue());
|
$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