diff --git a/src/AdamWathan/BootForms/BasicFormBuilder.php b/src/AdamWathan/BootForms/BasicFormBuilder.php index 6f173d0..aeafece 100644 --- a/src/AdamWathan/BootForms/BasicFormBuilder.php +++ b/src/AdamWathan/BootForms/BasicFormBuilder.php @@ -173,7 +173,9 @@ public function file($label, $name, $value = null) public function inputGroup($label, $name, $value = null) { $control = new InputGroup($name); - $control->value($value); + if (!is_null($value) || !is_null($value = $this->getValueFor($name))) { + $control->value($value); + } return $this->formGroup($label, $name, $control); } diff --git a/tests/BasicFormBuilderTest.php b/tests/BasicFormBuilderTest.php index 01dd96f..e379837 100644 --- a/tests/BasicFormBuilderTest.php +++ b/tests/BasicFormBuilderTest.php @@ -499,6 +499,65 @@ public function testRenderInputGroupChangeTypeWithBothAddon() $this->assertEquals($expected, $result); } + public function testRenderInputGroupWithValue() + { + $expected = '
'; + $result = $this->form->inputGroup('Test', 'test')->value('abc')->render(); + $this->assertEquals($expected, $result); + } + + public function testRenderInputGroupWithOldInput() + { + $oldInput = Mockery::mock('AdamWathan\Form\OldInput\OldInputInterface'); + $oldInput->shouldReceive('hasOldInput')->andReturn(true); + $oldInput->shouldReceive('getOldInput')->andReturn('xyz'); + + $this->builder->setOldInputProvider($oldInput); + + $expected = '
'; + $result = $this->form->inputGroup('Test', 'test')->render(); + $this->assertEquals($expected, $result); + } + + public function testRenderInputGroupWithOldInputAndDefaultValue() + { + $oldInput = Mockery::mock('AdamWathan\Form\OldInput\OldInputInterface'); + $oldInput->shouldReceive('hasOldInput')->andReturn(true); + $oldInput->shouldReceive('getOldInput')->andReturn('xyz'); + + $this->builder->setOldInputProvider($oldInput); + + $expected = '
'; + $result = $this->form->inputGroup('Test', 'test')->defaultValue('acb')->render(); + $this->assertEquals($expected, $result); + } + + public function testRenderInputGroupWithDefaultValue() + { + $expected = '
'; + $result = $this->form->inputGroup('Test', 'test')->defaultValue('acb')->render(); + $this->assertEquals($expected, $result); + } + + public function testRenderInputGroupWithOldInputAndError() + { + $oldInput = Mockery::mock('AdamWathan\Form\OldInput\OldInputInterface'); + $oldInput->shouldReceive('hasOldInput')->andReturn(true); + $oldInput->shouldReceive('getOldInput')->andReturn('abc'); + + $this->builder->setOldInputProvider($oldInput); + + $errorStore = Mockery::mock('AdamWathan\Form\ErrorStore\ErrorStoreInterface'); + $errorStore->shouldReceive('hasError')->andReturn(true); + $errorStore->shouldReceive('getError')->andReturn('Test is required.'); + + $this->builder->setErrorStore($errorStore); + + $expected = '

Test is required.

'; + $result = $this->form->inputGroup('Test', 'test')->render(); + $this->assertEquals($expected, $result); + } + private function getStubObject() { $obj = new stdClass; diff --git a/tests/InputGroupTest.php b/tests/InputGroupTest.php index 5e26b26..508cc86 100644 --- a/tests/InputGroupTest.php +++ b/tests/InputGroupTest.php @@ -35,4 +35,37 @@ public function testCanRenderAfterAddonAndType() $result = $input->render(); $this->assertEquals($expected, $result); } -} \ No newline at end of file + + public function testCanRenderWithValue() + { + $input = new InputGroup('test'); + $input = $input->value('abc'); + $expected = '
'; + $result = $input->render(); + $this->assertEquals($expected, $result); + + $input = new InputGroup('test'); + $input = $input->value(null); + $expected = '
'; + $result = $input->render(); + $this->assertEquals($expected, $result); + } + + public function testDefaultValue() + { + $input = new InputGroup('test'); + $expected = '
'; + $result = $input->defaultValue('abc')->render(); + $this->assertEquals($expected, $result); + + $input = new InputGroup('test'); + $expected = '
'; + $result = $input->value('xyz')->defaultValue('abc')->render(); + $this->assertEquals($expected, $result); + + $input = new InputGroup('test'); + $expected = '
'; + $result = $input->defaultValue('abc')->value('xyz')->render(); + $this->assertEquals($expected, $result); + } +}