mirror of
https://github.com/silverstripe/silverstripe-framework
synced 2024-10-22 14:05:37 +02:00
Merged revisions 47414 via svnmerge from
svn://svn.silverstripe.com/silverstripe/modules/sapphire/branches/2.2.0-mesq ........ r47414 | ischommer | 2007-12-20 18:17:40 +1300 (Thu, 20 Dec 2007) | 1 line added option to loadDataFrom() for loading values from nested arrays that PHP generated from URL-strings ........ git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/trunk@52184 467b73ca-7a2a-4603-9d3b-597d59a354a9
This commit is contained in:
parent
e2c4a2d730
commit
e0f6e50c82
@ -565,13 +565,20 @@ class Form extends ViewableData {
|
|||||||
|
|
||||||
$dataFields = $this->fields->dataFields();
|
$dataFields = $this->fields->dataFields();
|
||||||
if($dataFields) foreach($dataFields as $field) {
|
if($dataFields) foreach($dataFields as $field) {
|
||||||
|
$name = $field->Name();
|
||||||
if($name = $field->Name()) {
|
if($name) {
|
||||||
if($o) {
|
if($o) {
|
||||||
// this was failing with the field named 'Name'
|
// this was failing with the field named 'Name'
|
||||||
$val = $object->__get($name);
|
$val = $object->__get($name);
|
||||||
} else {
|
} else {
|
||||||
$val = isset($object[$name]) ? $object[$name] : null;
|
if(strpos($name,'[') && is_array($object) && !isset($object[$name])) {
|
||||||
|
// if field is in array-notation, we need to resolve the array-structure PHP creates from query-strings
|
||||||
|
preg_match('/' . addcslashes($name,'[]') . '=([^&]*)/', urldecode(http_build_query($object)), $matches);
|
||||||
|
$val = isset($matches[1]) ? $matches[1] : null;
|
||||||
|
} else {
|
||||||
|
// else we assume its a simple key-string
|
||||||
|
$val = isset($object[$name]) ? $object[$name] : null;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// First check looks for (fieldname)_unchanged, an indicator that we shouldn't overwrite the field value
|
// First check looks for (fieldname)_unchanged, an indicator that we shouldn't overwrite the field value
|
||||||
|
45
tests/forms/FormTest.php
Normal file
45
tests/forms/FormTest.php
Normal file
@ -0,0 +1,45 @@
|
|||||||
|
<?php
|
||||||
|
class FormTest extends SapphireTest {
|
||||||
|
|
||||||
|
public function testLoadDataFromRequest() {
|
||||||
|
$form = new Form(
|
||||||
|
new Controller(),
|
||||||
|
'Form',
|
||||||
|
new FieldSet(
|
||||||
|
new TextField('key1'),
|
||||||
|
new TextField('namespace[key2]'),
|
||||||
|
new TextField('namespace[key3][key4]'),
|
||||||
|
new TextField('othernamespace[key5][key6][key7]')
|
||||||
|
),
|
||||||
|
new FieldSet()
|
||||||
|
);
|
||||||
|
|
||||||
|
// url would be ?key1=val1&namespace[key2]=val2&namespace[key3][key4]=val4&othernamespace[key5][key6][key7]=val7
|
||||||
|
$requestData = array(
|
||||||
|
'key1' => 'val1',
|
||||||
|
'namespace' => array(
|
||||||
|
'key2' => 'val2',
|
||||||
|
'key3' => array(
|
||||||
|
'key4' => 'val4',
|
||||||
|
)
|
||||||
|
),
|
||||||
|
'othernamespace' => array(
|
||||||
|
'key5' => array(
|
||||||
|
'key6' =>array(
|
||||||
|
'key7' => 'val7'
|
||||||
|
)
|
||||||
|
)
|
||||||
|
)
|
||||||
|
);
|
||||||
|
|
||||||
|
$form->loadDataFrom($requestData);
|
||||||
|
|
||||||
|
$fields = $form->Fields();
|
||||||
|
$this->assertEquals($fields->fieldByName('key1')->Value(), 'val1');
|
||||||
|
$this->assertEquals($fields->fieldByName('namespace[key2]')->Value(), 'val2');
|
||||||
|
$this->assertEquals($fields->fieldByName('namespace[key3][key4]')->Value(), 'val4');
|
||||||
|
$this->assertEquals($fields->fieldByName('othernamespace[key5][key6][key7]')->Value(), 'val7');
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
?>
|
Loading…
Reference in New Issue
Block a user