From ebe5123467b8a12243e18e5e383918d102e52379 Mon Sep 17 00:00:00 2001 From: pazzoone Date: Thu, 14 May 2015 20:40:11 +0200 Subject: [PATCH 1/2] Bug on FromBuilder.php HOW TO REPRODUCE BUG: I have an m:m relation in my isolated form. This relation si rendered by a list of checkboxes. Checkboxes names are in the form form_name[relation_name][] BUG: Form checkboxes autofill functionalities works well only when: 1. On new form (method=get) 2. On posting some errors ERROR: On edit form data the autofill functionalities wont' work because $posted is a Collection, so my edit. --- src/FormBuilder.php | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/src/FormBuilder.php b/src/FormBuilder.php index f573c84..ac3afc3 100755 --- a/src/FormBuilder.php +++ b/src/FormBuilder.php @@ -676,7 +676,14 @@ class FormBuilder { $posted = $this->getValueAttribute($name); - return is_array($posted) ? in_array($value, $posted) : (bool) $posted; + + if (is_array($posted)) { + return in_array($value, $posted); + } else if ($posted instanceof \Illuminate\Database\Eloquent\Collection) { + return $posted->contains('id', $value); + } else { + return (bool) $posted; + } } /** From f6d0f1c5c4992ee7687976fc72b9061c077b2725 Mon Sep 17 00:00:00 2001 From: alexandre-butynski Date: Mon, 1 Jun 2015 18:57:42 +0200 Subject: [PATCH 2/2] Checkboxes work with relationships (with unit testing and proper formatting) --- src/FormBuilder.php | 23 ++++++++++++++--------- tests/FormBuilderTest.php | 27 ++++++++++++++++++++++++++- 2 files changed, 40 insertions(+), 10 deletions(-) diff --git a/src/FormBuilder.php b/src/FormBuilder.php index ac3afc3..3f2227b 100755 --- a/src/FormBuilder.php +++ b/src/FormBuilder.php @@ -4,6 +4,7 @@ use DateTime; use Illuminate\Routing\UrlGenerator; use Illuminate\Session\Store as Session; use Illuminate\Support\Traits\Macroable; +use Illuminate\Support\Collection; class FormBuilder { @@ -674,16 +675,20 @@ class FormBuilder { if ($this->missingOldAndModel($name)) return $checked; - $posted = $this->getValueAttribute($name); + $posted = $this->getValueAttribute($name, $checked); - - if (is_array($posted)) { - return in_array($value, $posted); - } else if ($posted instanceof \Illuminate\Database\Eloquent\Collection) { - return $posted->contains('id', $value); - } else { - return (bool) $posted; - } + if (is_array($posted)) + { + return in_array($value, $posted); + } + else if ($posted instanceof Collection) + { + return $posted->contains('id', $value); + } + else + { + return (bool) $posted; + } } /** diff --git a/tests/FormBuilderTest.php b/tests/FormBuilderTest.php index 2c9d3be..4c925d1 100644 --- a/tests/FormBuilderTest.php +++ b/tests/FormBuilderTest.php @@ -7,6 +7,7 @@ use Collective\Html\FormBuilder; use Collective\Html\HtmlBuilder; use Illuminate\Routing\UrlGenerator; use Illuminate\Routing\RouteCollection; +use Illuminate\Support\Collection; class FormBuilderTest extends PHPUnit_Framework_TestCase { @@ -368,6 +369,30 @@ class FormBuilderTest extends PHPUnit_Framework_TestCase { } + public function testFormCheckboxWithModelRelation() + { + $this->formBuilder->setSessionStore($session = m::mock('Illuminate\Session\Store')); + $session->shouldReceive('getOldInput')->withNoArgs()->andReturn(array()); + $session->shouldReceive('getOldInput')->with('items')->andReturn(null); + + $mockModel2 = new StdClass; + $mockModel2->id = 2; + $mockModel3 = new StdClass; + $mockModel3->id = 3; + $this->setModel(array('items' => new Collection(array($mockModel2, $mockModel3)))); + + $check1 = $this->formBuilder->checkbox('items[]', 1); + $check2 = $this->formBuilder->checkbox('items[]', 2); + $check3 = $this->formBuilder->checkbox('items[]', 3, false); + $check4 = $this->formBuilder->checkbox('items[]', 4, true); + + $this->assertEquals('', $check1); + $this->assertEquals('', $check2); + $this->assertEquals('', $check3); + $this->assertEquals('', $check4); + } + + public function testFormCheckboxWithoutSession() { $form1 = $this->formBuilder->checkbox('foo'); @@ -474,4 +499,4 @@ class FormBuilderModelStub { { return isset($this->data[$key]); } -} \ No newline at end of file +}