From eb8060dda6a2dd0ec3827252beebb9f65ca037fb Mon Sep 17 00:00:00 2001 From: "Sahan H." Date: Fri, 11 Oct 2013 16:17:58 +0530 Subject: [PATCH 1/2] Better default value handling Enable array based default value handling --- src/Jonob/Formly/Formly.php | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/src/Jonob/Formly/Formly.php b/src/Jonob/Formly/Formly.php index 4ec939d..4667f2a 100644 --- a/src/Jonob/Formly/Formly.php +++ b/src/Jonob/Formly/Formly.php @@ -88,6 +88,8 @@ private function loadConfig() */ public function setDefaults($defaults = array()) { + $defaults = json_decode(json_encode($defaults), true); + if (count($defaults) > 0) { $this->defaults = (object)$defaults; @@ -488,7 +490,10 @@ private function buildLabel($name, $label = '') private function calculateValue($name, $default = '', $radioValue = '') { $result = ''; - + + //make array named fields to dot notation + $field_name = str_replace(array('[', ']'), array('.', ''), $name); + // First check if there is post data // This assumes that you are redirecting after failed post // and that you have flashed the data @@ -508,11 +513,11 @@ private function calculateValue($name, $default = '', $radioValue = '') } // lastly, check if any defaults have been set for the form as a whole - elseif (isset($this->defaults->$name)) + elseif ($value = array_get($this->defaults, $field_name)) { $result = ($radioValue) - ? $this->defaults->$name == $radioValue - : $this->defaults->$name; + ? $value == $radioValue + : $value; } return $result; @@ -695,4 +700,4 @@ public function close() { return Form::close(); } -} \ No newline at end of file +} From 6d0e636a31edeb4c387f8312f30398d0ec31ec73 Mon Sep 17 00:00:00 2001 From: "Sahan H." Date: Fri, 11 Oct 2013 16:22:20 +0530 Subject: [PATCH 2/2] Disable object conversion of default values --- src/Jonob/Formly/Formly.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Jonob/Formly/Formly.php b/src/Jonob/Formly/Formly.php index 4667f2a..1f39677 100644 --- a/src/Jonob/Formly/Formly.php +++ b/src/Jonob/Formly/Formly.php @@ -92,7 +92,7 @@ public function setDefaults($defaults = array()) if (count($defaults) > 0) { - $this->defaults = (object)$defaults; + $this->defaults = $defaults; } return $this;