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('
'; } @@ -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 = ''; @@ -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 = '';