diff --git a/src/FormBuilder.php b/src/FormBuilder.php
index 23cbad5c..e2f7c4d4 100755
--- a/src/FormBuilder.php
+++ b/src/FormBuilder.php
@@ -4,6 +4,7 @@
use Illuminate\Routing\UrlGenerator;
use Illuminate\Session\Store as Session;
use Illuminate\Support\Traits\Macroable;
+use Illuminate\Support\Collection;
class FormBuilder {
@@ -757,9 +758,20 @@ protected function getCheckboxCheckedState($name, $value, $checked)
if($this->missingOldAndModel($name)) return $checked;
- $posted = $this->getValueAttribute($name);
+ $posted = $this->getValueAttribute($name, $checked);
- return is_array($posted) ? in_array($value, $posted) : (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 35f37d3b..3eb61ea0 100644
--- a/tests/FormBuilderTest.php
+++ b/tests/FormBuilderTest.php
@@ -7,6 +7,7 @@
use Collective\Html\HtmlBuilder;
use Illuminate\Routing\UrlGenerator;
use Illuminate\Routing\RouteCollection;
+use Illuminate\Support\Collection;
class FormBuilderTest extends PHPUnit_Framework_TestCase {
@@ -409,6 +410,30 @@ public function testFormCheckboxRepopulation()
}
+ 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');