Last active
December 4, 2015 13:20
-
-
Save wzulfikar/243a4db7f2d6c88e4032 to your computer and use it in GitHub Desktop.
example of manual validation and throwing its exception in laravel 5.
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
<?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