In Laravel 5.1 we can use $this->validate(...) method in a Controller, like this:
public function postLogin(Request $request)
{
$this->validate($request, [
'username' => 'required',
'password' => 'required',
]);
// ...
}
But if input is invalid the package breaks routing.
I have seen that the problem is solved for FormRequest (https://github.com/dingo/api/issues/462).
I think that can be solved overriding "validate" method of Illuminate\Foundation\Validation\ValidatesRequests ?
Is this the best approach?
Or extend the Dingo FormRequest class.
On Tue, 18 Aug 2015 03:45 Davide Bellini [email protected] wrote:
In Laravel 5.1 we can use $this->validate(...) method in a Controller,
like this:public function postLogin(Request $request){ $this->validate($request, [ 'username' => 'required', 'password' => 'required', ]); // ...}
But if input is invalid the package breaks routing.
I have seen that the problem is solved for FormRequest (#462
https://github.com/dingo/api/issues/462).I think that can be solved overriding "validate" method of
_Illuminate\Foundation\Validation\ValidatesRequests_ ?Is this the best approach?
—
Reply to this email directly or view it on GitHub
https://github.com/dingo/api/issues/584.
Thank you @jasonlewis
I preferred override "validate" method in the controller like this:
use Illuminate\Http\Request;
use Dingo\Api\Exception\ValidationHttpException;
use Illuminate\Foundation\Bus\DispatchesCommands;
use Illuminate\Routing\Controller as BaseController;
use Illuminate\Foundation\Validation\ValidatesRequests;
class ApiController extends BaseController
{
use DispatchesCommands, ValidatesRequests;
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());
}
}
}
it's good~
For future reference, I needed to add this line for it to work:
use Dingo\Api\Http\Request;
it's seem
"use Dingo\Api\HttpRequest;"
is not work for me when i attempt to login on admin panel in back-end.
the error says "Argument 1 passed to App\Http\Controllers\Controller::validate() must be an instance of Dingo\Api\HttpRequest, instance of Illuminate\HttpRequest given,"
so, i replace it with
use Illuminate\HttpRequest; as @billmn posted and it's work
You don't really need to override the validation method, since it already passes the $validator - at least in Lumen I just did this:
use Dingo\Api\Exception\ValidationHttpException;
class ApiController extends BaseController {
protected function throwValidationException(\Illuminate\Http\Request $request, $validator) {
throw new ValidationHttpException($validator->errors());
}
}
Also, the reason you're getting an error with using Dingo\Api\Http\Request is that the method you're overriding used the Illuminate\Http\Request and the methods have to match.
Most helpful comment
You don't really need to override the validation method, since it already passes the
$validator- at least in Lumen I just did this:Also, the reason you're getting an error with using
Dingo\Api\Http\Requestis that the method you're overriding used theIlluminate\Http\Requestand the methods have to match.