-
-
Save fhferreira/18836bc50b112e341316 to your computer and use it in GitHub Desktop.
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
# app/validation/AbstractValidator.php | |
<?php namespace YourApp\Validation; | |
/** | |
* Abstract implementation of a validator. | |
* | |
* @author Andrea Marco Sartori | |
*/ | |
abstract class AbstractValidator implements ValidatorInterface | |
{ | |
/** | |
* Validate the given input. | |
* | |
* @author Andrea Marco Sartori | |
* @param array $data | |
* @return void | |
*/ | |
public function validate($data) | |
{ | |
$validation = app('validator')->make($data, $this->getRules()); | |
if($validation->fails()) | |
{ | |
throw new Exception($validation->errors()); | |
} | |
} | |
/** | |
* Retrieve the validation rules. | |
* | |
* @author Andrea Marco Sartori | |
* @return array | |
*/ | |
abstract protected function getRules(); | |
} |
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
# app/validation/Exception.php | |
<?php namespace YourApp\Validation; | |
use Illuminate\Support\MessageBag; | |
/** | |
* Invalid input exception. | |
* | |
* @author Andrea Marco Sartori | |
*/ | |
class Exception extends \Exception | |
{ | |
/** | |
* @author Andrea Marco Sartori | |
* @var Illuminate\Support\MessageBag $errors Validation errors. | |
*/ | |
protected $errors; | |
/** | |
* Set the dependencies. | |
* | |
* @author Andrea Marco Sartori | |
* @param Illuminate\Support\MessageBag $errors | |
* @return void | |
*/ | |
public function __construct(MessageBag $errors) | |
{ | |
$this->errors = $errors; | |
parent::__construct(); | |
} | |
/** | |
* Retrieve the validation errors. | |
* | |
* @author Andrea Marco Sartori | |
* @return Illuminate\Support\MessageBag | |
*/ | |
public function getErrors() | |
{ | |
return $this->errors; | |
} | |
} |
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
# app/start/global.php | |
<?php | |
// add the following code to let Laravel know what to do | |
// when a validation exception is thrown | |
App::error(function(YourApp\Validation\Exception $exception) | |
{ | |
$errors = $exception->getErrors(); | |
return Redirect::back()->withInput()->withErrors($errors); | |
}); |
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
# app/controllers/RegistrationController.php | |
<?php | |
use YourApp\Validation\RegistrationValidator; | |
class RegistrationController extends BaseController { | |
/** | |
* @author Andrea Marco Sartori | |
* @var YourApp\Validation\RegistrationValidator $validator Registration input validator. | |
*/ | |
protected $validator; | |
/** | |
* Set the dependencies. | |
* | |
* @author Andrea Marco Sartori | |
* @param YourApp\Validation\RegistrationValidator $validator | |
* @return void | |
*/ | |
public function __construct(RegistrationValidator $validator) | |
{ | |
$this->validator = $validator; | |
} | |
/** | |
* Display a listing of the resource. | |
* | |
* @return Response | |
*/ | |
public function index() | |
{ | |
return View::make('registration'); | |
} | |
/** | |
* Store a newly created resource in storage. | |
* | |
* @return Response | |
*/ | |
public function store() | |
{ | |
$this->validator->validate(Input::all()); | |
// your registration logic here | |
// if validation fails, it will not be run | |
// because YourApp\Validation\Exception will be thrown | |
} | |
} |
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
# app/validation/RegistrationValidator.php | |
<?php namespace YourApp\Validation; | |
/** | |
* Validate the registration input. | |
* | |
* @author Andrea Marco Sartori | |
*/ | |
class RegistrationValidator extends AbstractValidator | |
{ | |
/** | |
* Retrieve the validation rules. | |
* | |
* @author Andrea Marco Sartori | |
* @return array | |
*/ | |
protected function getRules() | |
{ | |
return [ | |
'name' => 'required|alpha|min:3', | |
'email' => 'required|email|unique:users', | |
'password' => 'required|min:6|confirmed', | |
]; | |
} | |
} |
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
# app/validation/ValidatorInterface.php | |
<?php namespace YourApp\Validation; | |
/** | |
* Interface for validators. | |
* | |
* @author Andrea Marco Sartori | |
*/ | |
interface ValidatorInterface | |
{ | |
/** | |
* Validate the given input. | |
* | |
* @author Andrea Marco Sartori | |
* @param array $data | |
* @return void | |
*/ | |
public function validate($data); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment