Skip to content
This repository was archived by the owner on Feb 26, 2018. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions src/AdamWathan/Form/Elements/FormOpen.php
Original file line number Diff line number Diff line change
Expand Up @@ -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('<form%s>', $this->renderAttributes())];
Expand All @@ -25,6 +34,10 @@ public function render()
$tags[] = $this->hiddenMethod->render();
}

if ($this->hasHiddenNamespace()) {
$tags[] = $this->hiddenNamespace->render();
}

return implode($tags);
}

Expand All @@ -38,6 +51,11 @@ protected function hasHiddenMethod()
return isset($this->hiddenMethod);
}

protected function hasHiddenNamespace()
{
return isset($this->hiddenNamespace);
}

public function post()
{
$this->setMethod('POST');
Expand Down Expand Up @@ -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);
Expand Down
33 changes: 31 additions & 2 deletions src/AdamWathan/Form/FormBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ class FormBuilder

protected $boundData;

protected $namespace;

public function setOldInputProvider(OldInputInterface $oldInputProvider)
{
$this->oldInput = $oldInputProvider;
Expand All @@ -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);
Expand All @@ -64,6 +73,7 @@ protected function hasToken()
public function close()
{
$this->unbindData();
$this->resetNamespace();

return '</form>';
}
Expand Down Expand Up @@ -239,14 +249,19 @@ public function getError($name, $format = null)
return $message;
}

public function getCurrentNamespace()
{
return $this->namespace;
}

public function bind($data)
{
$this->boundData = new BoundData($data);
}

public function getValueFor($name)
{
if ($this->hasOldInput()) {
if ($this->hasOldInput() && $this->hasMatchingOldNamespace()) {
return $this->getOldInput($name);
}

Expand Down Expand Up @@ -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)) {
Expand All @@ -295,6 +319,11 @@ protected function unbindData()
$this->boundData = null;
}

protected function resetNamespace()
{
$this->namespace = null;
}

public function selectMonth($name)
{
$options = [
Expand Down
28 changes: 28 additions & 0 deletions tests/BindingTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 = '<input type="text" name="first_name" value="Jesse">';
$this->form->bind($object);
$result = (string) $this->form->text('first_name');
$this->assertEquals($expected, $result);

$expected = '<input type="text" name="first_name" value="John">';
$this->form->name('user')->open();
$this->form->bind($object);
$result = (string) $this->form->text('first_name');
$this->assertEquals($expected, $result);

$expected = '<input type="text" name="first_name" value="Jesse">';
$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();
Expand Down
17 changes: 17 additions & 0 deletions tests/FormBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,30 @@ public function testFormOpen()
$this->assertEquals($expected, $result);
}

public function testFormOpenWithNamespace()
{
$expected = '<form method="POST" action=""><input type="hidden" name="_namespace" value="tycho">';
$result = (string) $this->form->name('tycho')->open();
$this->assertEquals($expected, $result);
}

public function testCanCloseForm()
{
$expected = '</form>';
$result = (string) $this->form->close();
$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 = '<input type="text" name="email">';
Expand Down