Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
new: change folder structure, add trait and support to set messages
- Loading branch information
Showing
4 changed files
with
124 additions
and
46 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
<?php namespace Motty\Laravel\Validator\Exceptions; | ||
|
||
use Exception; | ||
use Illuminate\Support\MessageBag; | ||
|
||
class ValidateException extends Exception | ||
{ | ||
/** | ||
* Errors object | ||
* | ||
* @var \Illuminate\Support\MessageBag | ||
*/ | ||
protected $errors; | ||
|
||
/** | ||
* Create a new validate exception instance | ||
* | ||
* @param string $message | ||
* @param MessageBag $errors | ||
* @param integer $code | ||
* @param Exception|null $previous | ||
*/ | ||
public function __construct($message, MessageBag $errors, $code = 0, Exception $previous = null) | ||
{ | ||
$this->errors = $errors; | ||
|
||
parent::__construct($message, $code, $previous); | ||
} | ||
|
||
/** | ||
* Gets the errors object | ||
* | ||
* @return \Illuminate\Support\MessageBag | ||
*/ | ||
public function erros() | ||
{ | ||
return $this->errors; | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
<?php namespace Motty\Laravel\Validator\Traits; | ||
|
||
use Motty\Laravel\Validator\Contracts\Validator; | ||
use Motty\Laravel\Validator\Exceptions\ValidateException; | ||
|
||
trait Validatable | ||
{ | ||
/** | ||
* The errors MessageBag instance | ||
* | ||
* @var \Illuminate\Support\MessageBag | ||
*/ | ||
protected $errors; | ||
|
||
/** | ||
* Run the validation checks in the input data | ||
* | ||
* @param array $data | ||
* | ||
* @return bool | ||
* @throws Exception | ||
* @throws ValidateException | ||
*/ | ||
public function runValidationChecks(array $data) | ||
{ | ||
foreach ($this->validators as $validator) { | ||
if ($validator instanceof Validator) { | ||
if (!$validator->with($data)->validate()) { | ||
$this->errors->merge($validator->errors()); | ||
} | ||
} else { | ||
throw new Exception("{$validator} is not an instance of Motty\\Laravel\\Validator\\Contracts\\Validator"); | ||
} | ||
} | ||
|
||
if ($this->errors->isEmpty()) { | ||
return true; | ||
} | ||
|
||
throw new ValidateException('Validation failed', $this->errors); | ||
} | ||
|
||
/** | ||
* Return the errors message bag | ||
* | ||
* @return \Illuminate\Support\MessageBag | ||
*/ | ||
public function errors() | ||
{ | ||
return $this->errors; | ||
} | ||
} |