Skip to content

Instantly share code, notes, and snippets.

@wzulfikar
Last active December 4, 2015 13:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wzulfikar/243a4db7f2d6c88e4032 to your computer and use it in GitHub Desktop.
Save wzulfikar/243a4db7f2d6c88e4032 to your computer and use it in GitHub Desktop.
example of manual validation and throwing its exception in laravel 5.
<?php
/**
* It is manual because L5 base controller uses `ValidatesRequests` trait
* which helps a lot so that we only need to code `$this->validate($request, $rules, $messages)`
* from our controller (which extends L5 controller). see: http://laravel.com/docs/5.1/validation.
*/
public function store(Request $request){
// make the validator.
// $rules & $messages are associative array.
// see: http://laravel.com/docs/5.1/validation#manually-creating-validators
$validator = Validator:: make($request->all(), $rules, $messages);
// throw exception if fails (will throw HttpResponseException)
if ($validator->fails())
$this->throwValidationException( $request, $validator );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment