Problem login validation messages Default LARAVEL 5.2

3

I am trying to make a custom login module based on the default login (Route :: auth ()), but somehow, the messages corresponding to a 'required' validation rules appear, in both fields, email and password, the problem is that I do not know where the validation rules are being applied. This error also happens to me in the default login.

FORM

@section('contenido')
<div class="row">
    <div class="col-md-12">

    @if(count($errors) > 0)
        <div class="alert alert-danger alert-dismissible fade in" role=alert > 
                <button type="button" class="close" data-dismiss="alert" aria-label="Close">
                  <span aria-hidden="true">&times;</span>
                </button>
                <ul>
          @foreach($errors->all() as $errores)

              <li style="font-weight: bolder; list-style-type: none">{{$errores}}</li>
          @endforeach
           </ul>
         </div>       
        @endif


    </div>
</div>
<div class="container-fluid">
    <div class="row">
        <div class="col-md-12">

        @if(count($errors) > 0)
            <div class="alert alert-danger alert-dismissible fade in" role=alert > 
                    <button type="button" class="close" data-dismiss="alert" aria-label="Close">
                      <span aria-hidden="true">&times;</span>
                    </button>
                    <ul>
              @foreach($errors->all() as $errores)

                  <li style="font-weight: bolder; list-style-type: none">{{$errores}}</li>
              @endforeach
               </ul>
             </div>       
            @endif


        </div>
    </div>
</div>
<div id="superespacio"></div>
<div class="container">
    <div class="row">

         {{Form::open(array('url' => 'login', 'method'=>'POST','class'=>"form-horizontal"))}}

                          <div class="form-group">
                               {!! Form::label('Correo Electronico', null, array('class' => 'col-sm-2 control-label')) !!} 
                               <div class="col-sm-10">
                                  {!! Form::text('correo', null, array('class' => 'form-control','placeholder' =>'Introduce tu Correo Electronico')) !!}

                              </div>
                          </div>

                          <div class="form-group">
                           {!! Form::label('Contraseña', null, array('class' => 'col-sm-2 control-label')) !!} 
                           <div class="col-sm-10">
                              {!! Form::text('pass', null, array('class' => 'form-control','placeholder' =>'Introduce tu Contraseña')) !!}

                          </div>
                      </div>

                  <div class="form-group">
                    <div class="col-sm-offset-2 col-sm-10">
                         {!! Form::submit('Inicia Session', ['class'=>'btn btn-default']) !!}

                    </div>
                </div>
            {{ Form::close() }}


    </div>
</div>
<div id="superespacio"></div>
@endsection

MODULE AUTHCONTROLLER (THE ONE THAT COMES BY DEFAULT)

<

?php

namespace App\Http\Controllers\Auth;

use App\User;
use Validator;
use App\Http\Controllers\Controller;
use Illuminate\Foundation\Auth\ThrottlesLogins;
use Illuminate\Foundation\Auth\AuthenticatesAndRegistersUsers;

class AuthController extends Controller
{
    /*
    |--------------------------------------------------------------------------
    | Registration & Login Controller
    |--------------------------------------------------------------------------
    |
    | This controller handles the registration of new users, as well as the
    | authentication of existing users. By default, this controller uses
    | a simple trait to add these behaviors. Why don't you explore it?
    |
    */

    use AuthenticatesAndRegistersUsers, ThrottlesLogins;

    /**
     * Where to redirect users after login / registration.
     *
     * @var string
     */
    protected $redirectTo = '/';

    /**
     * Create a new authentication controller instance.
     *
     * @return void
     */
    public function __construct()
    {
        $this->middleware($this->guestMiddleware(), ['except' => 'logout']);
    }

    /**
     * Get a validator for an incoming registration request.
     *
     * @param  array  $data
     * @return \Illuminate\Contracts\Validation\Validator
     */
    protected function validator(array $data)
    {
        return Validator::make($data, [
           //'name' => 'required|max:255',
          //  'correo' => 'required|unique:users',
          //  'pass' => 'required|min:6',
            //|email|max:255
            //|confirmed
        ]);
    }

    /**
     * Create a new user instance after a valid registration.
     *
     * @param  array  $data
     * @return User
     */
    protected function create(array $data)
    {
        return User::create([
           // 'name' => $data['name'],
            'email' => $data['correo'],
            'password' => bcrypt($data['pass']),
        ]);
    }
}

As you can see in this module, I have deactivated all the validation rules, because even so, I try to validate the email and password.

In the AuthenticatesUsers trait I tried to comment on this line to see if that was why, same result

   protected function validateLogin(Request $request)
    {
        $this->validate($request, [
            //$this->loginUsername() => 'required', 'password' => 'required',
        ]);
    }

The route

Route::auth();
    Route::group(['middleware'=>'auth'], function() {
        Route::resource('usuarios', 'UsuariosController');
    });

The route works well, by giving the user link it sends you to the middleware, but as I say I do not go from login to home, in the validation screen, even filling in the fields, it puts the message of 'required'

/ *********************** AFTER EDITION ************** ****** USER MODEL

<?php

namespace App;

use Illuminate\Foundation\Auth\User as Authenticatable;

class User extends Authenticatable
{
    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */

     protected $table = 'users';
    protected $fillable = [
        'name', 'email', 'password','tipo',
    ];

    /**
     * The attributes that should be hidden for arrays.
     *
     * @var array
     */
    protected $hidden = [
        'password', 'remember_token',
    ];



    public function articulos($value='')
    {
      return $this->hasMany('App\modelos\articulo');
    }


}
    
asked by KurodoAkabane 09.06.2016 в 19:51
source

1 answer

2

What happens is that Laravel searches by default the fields email (or the property username in the class) and password , this can be seen in the trait AuthenticatesUsers :

public function login(Request $request)
{
    $this->validateLogin($request);
    ...
    $credentials = $this->getCredentials($request);
    ...
}

As you can see, the validation is the first action performed by the method, then the corresponding code, even in the same trait:

protected function validateLogin(Request $request)
{
    $this->validate($request, [
        $this->loginUsername() => 'required', 'password' => 'required',
    ]);
}

The username is obtained with a method also located in the same trait:

public function loginUsername()
{
    return property_exists($this, 'username') ? $this->username : 'email';
}

After the introduction, what can be done then?

There are many options, the simplest (maybe not the best) may be to overwrite the login method (or the validateLogin method, depending on how you want to do it), in your authentication driver:

protected function validateLogin(Request $request)
{
    $this->validate($request, [
        $this->loginUsername() => 'required', 'pass' => 'required',
    ]);
}

For this you also need to modify the loginUsername method a bit, which we will modify to its simplest form:

public function loginUsername()
{
    return 'correo';
}

After the validation, it is necessary to take into account the authentication as such, for which you need to work with the getCredentials method which is in the trait of which we have already spoken:

// versión original
protected function getCredentials(Request $request)
{
    return $request->only($this->loginUsername(), 'password');
}

In this method we would need to change the string 'password' by 'pass' .

    
answered by 10.06.2016 / 00:17
source