Created
October 17, 2018 12:57
-
-
Save harini-ua/80e828150072698d982e03925b8f1cc3 to your computer and use it in GitHub Desktop.
Get validation error messages in Lumen & Dingo API project
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 | |
namespace App\Http\Controllers; | |
use Illuminate\Http\Request; | |
use Dingo\Api\Exception\ValidationHttpException; | |
use Laravel\Lumen\Routing\Controller as BaseController; | |
class Controller extends BaseController | |
{ | |
/** | |
* Override validate method use dingo validation exception | |
* | |
* @param Request $request | |
* @param array $rules | |
* @param array $messages | |
* @param array $customAttributes | |
*/ | |
public function validate( | |
Request $request, | |
array $rules, | |
array $messages = [], | |
array $customAttributes = []) | |
{ | |
$validator = $this->getValidationFactory()->make( | |
$request->all(), | |
$rules, $messages, | |
$customAttributes | |
); | |
if ($validator->fails()) { | |
throw new ValidationHttpException( | |
$validator->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
<?php | |
use Illuminate\Http\Request; | |
class UserApiController extends Controller | |
{ | |
use Helpers; | |
function login(Request $request) | |
{ | |
// Validate request | |
$this->validate($request, [ | |
'username' => 'required|min:3', | |
'password' => 'required|min:3' | |
]); | |
// ... | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment