diff --git a/src/AdamWathan/Form/ErrorStore/ErrorStoreInterface.php b/src/AdamWathan/Form/ErrorStore/ErrorStoreInterface.php index b1e0906..2e36e4f 100644 --- a/src/AdamWathan/Form/ErrorStore/ErrorStoreInterface.php +++ b/src/AdamWathan/Form/ErrorStore/ErrorStoreInterface.php @@ -4,7 +4,11 @@ interface ErrorStoreInterface { + public function hasErrors(); + public function hasError($key); + public function getErrors(); + public function getError($key); } diff --git a/src/AdamWathan/Form/ErrorStore/IlluminateErrorStore.php b/src/AdamWathan/Form/ErrorStore/IlluminateErrorStore.php index 00052e2..57ee1d1 100644 --- a/src/AdamWathan/Form/ErrorStore/IlluminateErrorStore.php +++ b/src/AdamWathan/Form/ErrorStore/IlluminateErrorStore.php @@ -35,12 +35,12 @@ public function getError($key) return $this->getErrors()->first($key); } - protected function hasErrors() + public function hasErrors() { return $this->session->has('errors'); } - protected function getErrors() + public function getErrors() { return $this->hasErrors() ? $this->session->get('errors') : null; } diff --git a/src/AdamWathan/Form/FormBuilder.php b/src/AdamWathan/Form/FormBuilder.php index ab97569..56aba08 100644 --- a/src/AdamWathan/Form/FormBuilder.php +++ b/src/AdamWathan/Form/FormBuilder.php @@ -191,6 +191,11 @@ public function token() return $token; } + public function hasErrors() + { + return $this->errorStore->hasErrors(); + } + public function hasError($name) { if (! isset($this->errorStore)) { @@ -200,6 +205,15 @@ public function hasError($name) return $this->errorStore->hasError($name); } + public function getErrors() + { + if(! $this->hasErrors()){ + return null; + } + + return $this->errorStore->getErrors()->all(); + } + public function getError($name, $format = null) { if (! isset($this->errorStore)) { diff --git a/tests/FormBuilderTest.php b/tests/FormBuilderTest.php index d32b934..b182410 100644 --- a/tests/FormBuilderTest.php +++ b/tests/FormBuilderTest.php @@ -1,6 +1,7 @@ assertFalse($result); } + public function testCanCheckForErrorMessages() + { + $errorStore = Mockery::mock('AdamWathan\Form\ErrorStore\ErrorStoreInterface'); + $errorStore->shouldReceive('hasErrors')->andReturn(true); + + $this->form->setErrorStore($errorStore); + + $this->assertTrue($this->form->hasErrors()); + + $errorStore = Mockery::mock('AdamWathan\Form\ErrorStore\ErrorStoreInterface'); + $errorStore->shouldReceive('hasErrors')->andReturn(false); + + $this->form->setErrorStore($errorStore); + + $this->assertFalse($this->form->hasErrors()); + } + public function testCanRetrieveErrorMessage() { $errorStore = Mockery::mock('AdamWathan\Form\ErrorStore\ErrorStoreInterface'); @@ -195,6 +213,30 @@ public function testCanRetrieveErrorMessage() $this->assertEquals($expected, $result); } + public function testCanRetrieveErrorMessages() + { + $errors = new MessageBag(['foo.bar' => 'Some error']); + $errorStore = Mockery::mock('AdamWathan\Form\ErrorStore\ErrorStoreInterface'); + $errorStore->shouldReceive('hasErrors')->andReturn(true); + $errorStore->shouldReceive('getErrors')->andReturn($errors); + + $this->form->setErrorStore($errorStore); + + $expected = $errors->all(); + $result = $this->form->getErrors(); + $this->assertEquals($expected, $result); + + $errorStore = Mockery::mock('AdamWathan\Form\ErrorStore\ErrorStoreInterface'); + $errorStore->shouldReceive('hasErrors')->andReturn(false); + $errorStore->shouldReceive('getErrors')->andReturn(null); + + $this->form->setErrorStore($errorStore); + + $expected = null; + $result = $this->form->getErrors(); + $this->assertEquals($expected, $result); + } + public function testCanRetrieveFormattedErrorMessage() { $errorStore = Mockery::mock('AdamWathan\Form\ErrorStore\ErrorStoreInterface'); diff --git a/tests/IlluminateErrorStoreTest.php b/tests/IlluminateErrorStoreTest.php index 7e5d025..b0269fd 100644 --- a/tests/IlluminateErrorStoreTest.php +++ b/tests/IlluminateErrorStoreTest.php @@ -15,4 +15,17 @@ public function test_it_converts_array_keys_to_dot_notation() $errorStore = new IlluminateErrorStore($session); $this->assertTrue($errorStore->hasError('foo[bar]')); } + + public function test_it_returns_all_errors() + { + $errors = new MessageBag(['foo.bar' => 'Some error']); + $session = Mockery::mock('Illuminate\Session\Store'); + $session->shouldReceive('has')->with('errors')->andReturn(true); + $session->shouldReceive('get')->with('errors')->andReturn($errors); + + $errorStore = new IlluminateErrorStore($session); + $this->assertTrue($errorStore->hasErrors()); + $this->assertEquals($errorStore->getErrors(), $errors); + } + }