diff --git a/src/AdamWathan/Form/Elements/FormOpen.php b/src/AdamWathan/Form/Elements/FormOpen.php index 73992d2..0bd8528 100644 --- a/src/AdamWathan/Form/Elements/FormOpen.php +++ b/src/AdamWathan/Form/Elements/FormOpen.php @@ -13,6 +13,15 @@ class FormOpen extends Element protected $hiddenMethod; + protected $hiddenNamespace; + + public function __construct($namespace = null) + { + if ($namespace) { + $this->setHiddenNamespace($namespace); + } + } + public function render() { $tags = [sprintf('', $this->renderAttributes())]; @@ -25,6 +34,10 @@ public function render() $tags[] = $this->hiddenMethod->render(); } + if ($this->hasHiddenNamespace()) { + $tags[] = $this->hiddenNamespace->render(); + } + return implode($tags); } @@ -38,6 +51,11 @@ protected function hasHiddenMethod() return isset($this->hiddenMethod); } + protected function hasHiddenNamespace() + { + return isset($this->hiddenNamespace); + } + public function post() { $this->setMethod('POST'); @@ -84,6 +102,14 @@ protected function setHiddenMethod($method) return $this; } + protected function setHiddenNamespace($namespace) + { + $this->hiddenNamespace = new Hidden('_namespace'); + $this->hiddenNamespace->value($namespace); + + return $this; + } + public function setMethod($method) { $this->setAttribute('method', $method); diff --git a/src/AdamWathan/Form/FormBuilder.php b/src/AdamWathan/Form/FormBuilder.php index b336ffb..9d78589 100644 --- a/src/AdamWathan/Form/FormBuilder.php +++ b/src/AdamWathan/Form/FormBuilder.php @@ -30,6 +30,8 @@ class FormBuilder protected $boundData; + protected $namespace; + public function setOldInputProvider(OldInputInterface $oldInputProvider) { $this->oldInput = $oldInputProvider; @@ -45,9 +47,16 @@ public function setToken($token) $this->csrfToken = $token; } + public function name($namespace) + { + $this->namespace = $namespace; + + return $this; + } + public function open() { - $open = new FormOpen; + $open = new FormOpen($this->namespace); if ($this->hasToken()) { $open->token($this->csrfToken); @@ -64,6 +73,7 @@ protected function hasToken() public function close() { $this->unbindData(); + $this->resetNamespace(); return ''; } @@ -239,6 +249,11 @@ public function getError($name, $format = null) return $message; } + public function getCurrentNamespace() + { + return $this->namespace; + } + public function bind($data) { $this->boundData = new BoundData($data); @@ -246,7 +261,7 @@ public function bind($data) public function getValueFor($name) { - if ($this->hasOldInput()) { + if ($this->hasOldInput() && $this->hasMatchingOldNamespace()) { return $this->getOldInput($name); } @@ -281,6 +296,15 @@ protected function getBoundValue($name, $default) return $this->escape($this->boundData->get($name, $default)); } + protected function hasMatchingOldNamespace() + { + if (! $this->getCurrentNamespace()) { + return true; + } + + return $this->getOldInput('_namespace') === $this->getCurrentNamespace(); + } + protected function escape($value) { if (! is_string($value)) { @@ -295,6 +319,11 @@ protected function unbindData() $this->boundData = null; } + protected function resetNamespace() + { + $this->namespace = null; + } + public function selectMonth($name) { $options = [ diff --git a/tests/BindingTest.php b/tests/BindingTest.php index d350e83..f632208 100644 --- a/tests/BindingTest.php +++ b/tests/BindingTest.php @@ -395,6 +395,34 @@ public function testOldInputTakesPrecedenceOverBinding() $this->assertEquals($expected, $result); } + public function testNamespacedOldInputTakesPrecedenceOverBinding() + { + $oldInput = Mockery::mock('AdamWathan\Form\OldInput\OldInputInterface'); + $oldInput->shouldReceive('hasOldInput')->andReturn(true); + $oldInput->shouldReceive('getOldInput')->with('_namespace')->andReturn('profile'); + $oldInput->shouldReceive('getOldInput')->with('first_name')->andReturn('Jesse'); + $this->form->setOldInputProvider($oldInput); + + $object = $this->getStubObject(); + + $expected = ''; + $this->form->bind($object); + $result = (string) $this->form->text('first_name'); + $this->assertEquals($expected, $result); + + $expected = ''; + $this->form->name('user')->open(); + $this->form->bind($object); + $result = (string) $this->form->text('first_name'); + $this->assertEquals($expected, $result); + + $expected = ''; + $this->form->name('profile')->open(); + $this->form->bind($object); + $result = (string) $this->form->text('first_name'); + $this->assertEquals($expected, $result); + } + public function testExplicitUncheckOnCheckboxTakesPrecedenceOverBinding() { $object = $this->getStubObject(); diff --git a/tests/FormBuilderTest.php b/tests/FormBuilderTest.php index a3904ae..e827d49 100644 --- a/tests/FormBuilderTest.php +++ b/tests/FormBuilderTest.php @@ -26,6 +26,13 @@ public function testFormOpen() $this->assertEquals($expected, $result); } + public function testFormOpenWithNamespace() + { + $expected = '
'; + $result = (string) $this->form->name('tycho')->open(); + $this->assertEquals($expected, $result); + } + public function testCanCloseForm() { $expected = '
'; @@ -33,6 +40,16 @@ public function testCanCloseForm() $this->assertEquals($expected, $result); } + public function testFormCloseResetsNamespace() + { + $this->form->name('tycho')->open(); + $this->form->close(); + + $expected = null; + $result = $this->form->getCurrentNamespace(); + $this->assertEquals($expected, $result); + } + public function testTextBox() { $expected = '';