From 1b09282378111574302b032e1f27ccb4907e8569 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C4=BDubom=C3=ADr=20Jesze?= Date: Mon, 5 Aug 2013 18:15:24 +0200 Subject: [PATCH] Fix form populating with array names --- FormBuilder.php | 33 ++++++++++++++++++++++++--------- 1 file changed, 24 insertions(+), 9 deletions(-) diff --git a/FormBuilder.php b/FormBuilder.php index fd98299..2277e08 100755 --- a/FormBuilder.php +++ b/FormBuilder.php @@ -596,7 +596,7 @@ class FormBuilder { switch ($type) { case 'checkbox': - return $this->getCheckboxCheckedState($name, $checked); + return $this->getCheckboxCheckedState($name, $value, $checked); case 'radio': return $this->getRadioCheckedState($name, $value, $checked); @@ -614,13 +614,17 @@ class FormBuilder { * @param bool $checked * @return bool */ - protected function getCheckboxCheckedState($name, $checked) + protected function getCheckboxCheckedState($name, $value, $checked) { if ( ! $this->oldInputIsEmpty() and is_null($this->old($name))) return false; if ($this->missingOldAndModel($name)) return $checked; - return (bool) $this->getValueAttribute($name); + $valueAttribute = $this->getValueAttribute($name); + + if (is_array($valueAttribute)) return in_array($value, $valueAttribute); + + return (bool) $valueAttribute; } /** @@ -870,14 +874,14 @@ class FormBuilder { { if (is_null($name)) return $value; - if (isset($this->session) and $this->session->hasOldInput($name)) + if ( ! is_null($this->old($name))) { - return $this->session->getOldInput($name); + return $this->old($name); } if ( ! is_null($value)) return $value; - if (isset($this->model) and isset($this->model[$name])) + if (isset($this->model)) { return $this->getModelValueAttribute($name); } @@ -893,11 +897,11 @@ class FormBuilder { { if (is_object($this->model)) { - return object_get($this->model, $name); + return object_get($this->model, $this->transformKey($name)); } elseif (is_array($this->model)) { - return array_get($this->model, $name); + return array_get($this->model, $this->transformKey($name)); } } @@ -911,7 +915,7 @@ class FormBuilder { { if (isset($this->session)) { - return $this->session->getOldInput($name); + return $this->session->getOldInput($this->transformKey($name)); } } @@ -925,6 +929,17 @@ class FormBuilder { return (isset($this->session) and count($this->session->getOldInput()) == 0); } + /** + * Transform key from array to dot syntax. + * + * @param string $key + * @return string + */ + protected function transformKey($key) + { + return str_replace(array('.', '[]', '[', ']'), array('_', '', '.', ''), $key); + } + /** * Get the session store implementation. *