Skip to content

Commit

Permalink
Exposes the ability to see the reasons for validation failure
Browse files Browse the repository at this point in the history
  • Loading branch information
Jaspaul committed Aug 29, 2017
1 parent 6dda41a commit f094797
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 5 deletions.
7 changes: 7 additions & 0 deletions src/Contracts/Validatable.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,11 @@ public function isInvalid() : bool;
* @return \Illuminate\Contracts\Support\MessageProvider
*/
public function getErrors() : MessageProvider;

/**
* Returns the reasons why the validator failed.
*
* @return array
*/
public function getValidationFailureReasons() : array;
}
33 changes: 28 additions & 5 deletions src/Traits/Validates.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,13 @@ protected function getMessages() : array
return [];
}

/**
* The validator.
*
* @return \Illuminate\Contracts\Validation\Validator
*/
private $validator;

/**
* Returns an instance of the validator from our container.
*
Expand All @@ -52,11 +59,14 @@ private function getValidationFactory() : Factory
*/
private function getValidator() : Validator
{
return $this->getValidationFactory()->make(
$this->getData(),
$this->getRules(),
$this->getMessages()
);
if (is_null($this->validator)) {
$this->validator = $this->getValidationFactory()->make(
$this->getData(),
$this->getRules(),
$this->getMessages()
);
}
return $this->validator;
}

/**
Expand Down Expand Up @@ -106,6 +116,19 @@ public function getErrors() : MessageProvider
return $this->getValidator()->getMessageBag();
}

/**
* Returns the reasons why the validator failed.
*
* @return array
*/
public function getValidationFailureReasons() : array
{
if ($this->isInvalid()) {
return $this->getValidator()->failed();
}
return [];
}

/**
* Save the model to the database.
*
Expand Down
9 changes: 9 additions & 0 deletions tests/InvalidModelTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,4 +52,13 @@ public function testValidateThrowsAValidationException()
$model = new InvalidModel();
$model->validate();
}

public function testGetValidationFailureReasonsReturnsTheReasonsWhyTheValidationFailed()
{
$model = new InvalidModel();
$reasons = $model->getValidationFailureReasons();

$this->assertTrue(array_key_exists('email', $reasons));
$this->assertTrue(array_key_exists('Required', $reasons['email']));
}
}
6 changes: 6 additions & 0 deletions tests/ValidModelTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,10 @@ public function testValidateDoesNotThrowAValidationException()
$model = new ValidModel();
$this->assertVoid($model->validate());
}

public function testGetValidationFailureReasonsReturnsAnEmptyArray()
{
$model = new ValidModel();
$this->assertEmpty($model->getValidationFailureReasons());
}
}

0 comments on commit f094797

Please sign in to comment.