From 5248be92268e343dc0bacafd4c15de0c79d1f592 Mon Sep 17 00:00:00 2001 From: Daniel Hensby Date: Thu, 8 Dec 2016 16:46:15 +0000 Subject: [PATCH 1/4] FIX Handle fields with square brackets --- forms/Form.php | 33 +++++++++++++++++++++++++-------- 1 file changed, 25 insertions(+), 8 deletions(-) diff --git a/forms/Form.php b/forms/Form.php index 403195fe5..cb9d62785 100644 --- a/forms/Form.php +++ b/forms/Form.php @@ -1418,6 +1418,7 @@ class Form extends RequestHandler { if(is_object($data)) $this->record = $data; // dont include fields without data + /** @var FormField[] $dataFields */ $dataFields = $this->Fields()->dataFields(); if($dataFields) foreach($dataFields as $field) { $name = $field->getName(); @@ -1450,15 +1451,31 @@ class Form extends RequestHandler { $val = $data[$name]; } // If field is in array-notation we need to access nested data - else if(strpos($name,'[')) { - // First encode data using PHP's method of converting nested arrays to form data - $flatData = urldecode(http_build_query($data)); - // Then pull the value out from that flattened string - preg_match('/' . addcslashes($name,'[]') . '=([^&]*)/', $flatData, $matches); + else if(preg_match_all('/(.*)\[(.*)\]/U', $name, $matches)) { + //discard first match which is just the whole string + array_shift($matches); - if (isset($matches[1])) { - $exists = true; - $val = $matches[1]; + $keys = array_pop($matches); + $name = array_shift($matches); + $name = array_shift($name); + + if (array_key_exists($name, $data)) { + $tmpData = &$data[$name]; + // drill down into the data array looking for the corresponding value + foreach ($keys as $arrayKey) { + if ($arrayKey !== '') { + $tmpData = &$tmpData[$arrayKey]; + } else { + //empty square brackets means new array + if (is_array($tmpData)) { + $tmpData = array_shift($tmpData); + } + } + } + if ($tmpData) { + $val = $tmpData; + $exists = true; + } } } } From f8132b39af926bc8a77181db1c684b5b9c3458b2 Mon Sep 17 00:00:00 2001 From: Daniel Hensby Date: Mon, 12 Dec 2016 13:29:08 +0000 Subject: [PATCH 2/4] Assertions should be $this->assert($expected, $actual) --- tests/forms/FormTest.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/tests/forms/FormTest.php b/tests/forms/FormTest.php index 3ec2a9490..910bbc0ce 100644 --- a/tests/forms/FormTest.php +++ b/tests/forms/FormTest.php @@ -58,10 +58,10 @@ class FormTest extends FunctionalTest { $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'); + $this->assertEquals('val1', $fields->fieldByName('key1')->Value()); + $this->assertEquals('val2', $fields->fieldByName('namespace[key2]')->Value()); + $this->assertEquals('val4', $fields->fieldByName('namespace[key3][key4]')->Value()); + $this->assertEquals('val7', $fields->fieldByName('othernamespace[key5][key6][key7]')->Value()); } public function testSubmitReadonlyFields() { From 18ff6bec6da394675811458ecd75f863ac3b83c7 Mon Sep 17 00:00:00 2001 From: Myles Derham Date: Fri, 16 Dec 2016 10:40:01 +1300 Subject: [PATCH 3/4] Updated location of custom field templates --- docs/en/02_Developer_Guides/03_Forms/03_Form_Templates.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/docs/en/02_Developer_Guides/03_Forms/03_Form_Templates.md b/docs/en/02_Developer_Guides/03_Forms/03_Form_Templates.md index 7a4bf0c9f..64fae0889 100644 --- a/docs/en/02_Developer_Guides/03_Forms/03_Form_Templates.md +++ b/docs/en/02_Developer_Guides/03_Forms/03_Form_Templates.md @@ -14,7 +14,7 @@ can be rendered out using custom templates using `setTemplate`. $field = new TextField(..); $field->setTemplate('MyCustomTextField'); -Both `MyCustomTemplate.ss` and `MyCustomTextField.ss` should be located in **mysite/templates/Includes/** +Both `MyCustomTemplate.ss` and `MyCustomTextField.ss` should be located in **mysite/templates/forms/** or the same directory as the core.
It's recommended to copy the contents of the template you're going to replace and use that as a start. For instance, if @@ -66,4 +66,4 @@ well as the available syntax, see the [Templates](../templates) documentation. ## API Documentation * [api:Form] -* [api:FormField] \ No newline at end of file +* [api:FormField] From c007e85d1b9a1affd0ea7646b6a8c37d78b4450c Mon Sep 17 00:00:00 2001 From: Damian Mooyman Date: Fri, 16 Dec 2016 16:51:15 +1300 Subject: [PATCH 4/4] BUG Suppress HtmlEditorField casting Fixes #6396 --- forms/HtmlEditorField.php | 8 -------- forms/TextareaField.php | 2 +- tests/forms/HtmlEditorFieldTest.php | 14 ++++++++++++++ tests/forms/TextareaFieldTest.php | 14 ++++++++++++++ 4 files changed, 29 insertions(+), 9 deletions(-) diff --git a/forms/HtmlEditorField.php b/forms/HtmlEditorField.php index 725464114..e088b8401 100644 --- a/forms/HtmlEditorField.php +++ b/forms/HtmlEditorField.php @@ -26,14 +26,6 @@ class HtmlEditorField extends TextareaField { */ private static $sanitise_server_side = false; - /** - * @config - * @var array - */ - private static $casting = array( - 'Value' => 'HTMLText', - ); - protected $rows = 30; /** diff --git a/forms/TextareaField.php b/forms/TextareaField.php index 7b9df32a6..43030b55b 100644 --- a/forms/TextareaField.php +++ b/forms/TextareaField.php @@ -20,7 +20,7 @@ class TextareaField extends FormField { private static $casting = array( - 'Value' => 'HTMLText', + 'Value' => 'HTMLText(array("shortcodes" => 0))', ); /** diff --git a/tests/forms/HtmlEditorFieldTest.php b/tests/forms/HtmlEditorFieldTest.php index 726666cfd..6804334ec 100644 --- a/tests/forms/HtmlEditorFieldTest.php +++ b/tests/forms/HtmlEditorFieldTest.php @@ -15,6 +15,20 @@ class HtmlEditorFieldTest extends FunctionalTest { protected $extraDataObjects = array('HtmlEditorFieldTest_Object'); + public function testCasting() { + // Test special characters + $inputText = "These are some unicodes: ä, ö, & ü"; + $field = new HTMLEditorField("Test", "Test"); + $field->setValue($inputText); + $this->assertContains('These are some unicodes: ä, ö, & ü', $field->Field()); + + // Test shortcodes + $inputText = "Shortcode: [file_link id=4]"; + $field = new HTMLEditorField("Test", "Test"); + $field->setValue($inputText); + $this->assertContains('Shortcode: [file_link id=4]', $field->Field()); + } + public function testBasicSaving() { $obj = new HtmlEditorFieldTest_Object(); $editor = new HtmlEditorField('Content'); diff --git a/tests/forms/TextareaFieldTest.php b/tests/forms/TextareaFieldTest.php index d635574c1..6bb995ba2 100644 --- a/tests/forms/TextareaFieldTest.php +++ b/tests/forms/TextareaFieldTest.php @@ -2,6 +2,20 @@ class TextareaFieldTest extends SapphireTest { + public function testCasting() { + // Test special characters + $inputText = "These are some unicodes: ä, ö, & ü"; + $field = new TextareaField("Test", "Test"); + $field->setValue($inputText); + $this->assertContains('These are some unicodes: ä, ö, & ü', $field->Field()); + + // Test shortcodes + $inputText = "Shortcode: [file_link id=4]"; + $field = new TextareaField("Test", "Test"); + $field->setValue($inputText); + $this->assertContains('Shortcode: [file_link id=4]', $field->Field()); + } + /** * Quick smoke test to ensure that text with unicodes is being displayed properly in readonly fields. */