Support the ongoing development of Laravel.io →
Security Requests Validation
Last updated 2 years ago.
0

Hi All,

i figure out how to implement this request validation, on my API controller, i just write some lines as described at :

http://laravel.com/docs/5.1/validation#other-validation-approaches

and my login method becode like this :

use App\Http\Controllers\APIController;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\model\agents;
use Validator;

class AgentController extends APIController
{

 public function login(Request $request)
    {
        $validator = Validator::make($request->all(), [
            'email' => 'required|email|max:255',
            'password' => 'required|max:255',
        ]);

        if ($validator->fails()) {
            $message = $validator->errors();
            $this->SetStatusCode(404);
            return $this->RespondWithError($message);
        }
Last updated 8 years ago.
0

Hi Vidi,

As you know, there are many ways to accomplish same task in Laravel, and Laravel is keep evolving. When we write a Laravel application the goal is to keep controllers as clean as possible, and code reusability as much as possible (at least for me).

So with Laravel 5.1 you actually do not need to check if the validator has failed, Laravel will do that and send back all the necessary messages to the view in the $errors object. Meaning with your code you can simplify as such:

  public function login(Request $request)
       {
          $this->validate($request, [
               'email'  => 'required|email|max:255',
               'password' => 'required|max:255'
          ]);
       }

*If any of the validation fails, Laravel sends the request back to the view with $errors object *

That is all you need from your code to send back the error. Also when you're logging user, you probably want to validate for required only, everything else is not necessary for login.

Furthermore, i personally take keeping my controller very clean very serious, so instead of using default Request, i create custom request for all my forms and complete all the validation within the CustomRequest. In your case, i would have created a request called LoginRequest(), import my LoginRequest class and use it on my login method, as result, i do not have to do any validation within my controller.

I hope this helps.

0

@sp01010011 thank you very much my friend, and i will take your advice..

0

You must add X-Requested-With to header like below:

Content-Type : application/json

X-Requested-With : XMLHttpRequest

With XMLHttpRequest, laravel can received data from Postman. Hope this help you

Last updated 7 years ago.
0

langocthach said:

You must add X-Requested-With to header like below:

Content-Type : application/json

X-Requested-With : XMLHttpRequest

With XMLHttpRequest, laravel can received data from Postman. Hope this help you

Thanks @langocthach ! That was exactly what I was looking for.

0

Sign in to participate in this thread!

Eventy

Your banner here too?

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2024 Laravel.io - All rights reserved.