Checkboxes work with relationships (with unit testing and proper formatting)
This commit is contained in:
1 parent
ebe5123467
commit
f6d0f1c5c4
2 files changed
+40
-10
No files matched your search
+14
-9
@@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -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('<input name="items[]" type="checkbox" value="1">', $check1);
|
||||
$this->assertEquals('<input checked="checked" name="items[]" type="checkbox" value="2">', $check2);
|
||||
$this->assertEquals('<input name="items[]" type="checkbox" value="3">', $check3);
|
||||
$this->assertEquals('<input checked="checked" name="items[]" type="checkbox" value="4">', $check4);
|
||||
}
|
||||
|
||||
|
||||
public function testFormCheckboxWithoutSession()
|
||||
{
|
||||
$form1 = $this->formBuilder->checkbox('foo');
|
||||
@@ -474,4 +499,4 @@ class FormBuilderModelStub {
|
||||
{
|
||||
return isset($this->data[$key]);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user