Home > front end >  Laravel Error to inicialice ValidationFactory
Laravel Error to inicialice ValidationFactory

Time:10-06

I'm making my login system, through the LoginController controller, which in turn calls a request called loginRequest [I still have to update some names to Pascal]

My request only has two rules that username and password are required.

Then in a function getCredentials() I capture the username of this and validate that it is an email or not, this to give the user the option to log in both ways.

To identify if the user is actually an email, create a method 'isMail' in which I establish $factory with the content validated through an alias set to Validacion\Factory , ValidationFactory, but when executing the submit button it throws me the error :

Target [Illuminate\contracts\Validation\Factory] is not instantiable.

Could you help me?

template [login.blade.php]:

@extends('components\header')
<section >
  <div >
    <div >
      <div >
          <img src="{{ URL::asset('img/logo.png') }}"
           height="500">
      </div>
      <div >
        <form action="{{route('samein.login')}}" method="POST">
          @csrf
          <!-- Email input -->
          <div >
            <input type="text" name="username" 
              placeholder="Usuario" />
            <label  for="form3Example3">Usuario</label>
          </div>

          <!-- Password input -->
          <div >
            <input type="password" name="password" 
              placeholder="Contraseña" />
            <label  for="form3Example4">Contraseña</label>
          </div>

          <div >
            <button type="submit" 
              style="padding-left: 2.5rem; padding-right: 2.5rem;">Iniciar Sesión</button>
          </div>

        </form>
      </div>
    </div>
  </div>
</section>

controller[LoginController.login]

 <?php

namespace App\Http\Controllers;
use App\Http\Requests\loginrequest;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Auth;
use App\Models\User;
use Illuminate\Queue\RedisQueue;

class LoginController extends Controller
{

    public function index(){
        return view('login');
    }

    public function login( loginrequest $request ){

        $credentiales = $request->getCredentials();
        if( Auth::validate($credentiales) ){
            return redirect()->to('login')->withErrors('auth.failed');
        }

        $user = Auth::getProvider()->retrieveByCredentials($credentiales);

        Auth::login($user);
        return $this->authenticated($request,$user);
    }

    public function authenticated (Request $request,$user){
        return redirect('accountModule.indexusers');
    }
}

and my Request[loginrequst.php]

use Illuminate\Foundation\Http\FormRequest;
use Illuminate\contracts\Validation\Factory as ValidationFactory;

class loginrequest extends FormRequest
{
    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize()
    {
        return true;
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array<string, mixed>
     */
    public function rules()
    {
        return [
            //
            'username'=>'required',
            'password'=>'required'
        ];
    }

    public function getCredentials(){
        $username = $this->get('username');

        if( $this->isMail($username) ){ 
            return['email'=> $username,
                    'password' => $this->get('password')
            ];
         }
         return $this->only('username','password');
    }

    public function isMail($value){
        $factory = $this->container->make(ValidationFactory::class);
        
        return !$factory->make(['username'=>$value],['username'=>'email'])->fails();
    }
}

CodePudding user response:

I was reading your problem and I found interesting the way you plan to check if the email is there or not. I know it doesn't answer your question but you could try the following:

In your Request:

public function rules()
{
    return [
        //
        'username'=>'required',
        'password'=>'required'
    ];
}

In your controller:

public function login( loginrequest $request ){

    $field = filter_var($request->input('username'), FILTER_VALIDATE_EMAIL) ? 'email' : 'username';
    $request->merge([$field => $request->input('username')]);

    //validate credentials
    if (!Auth::validate($request->only($field, 'password'))) {
        return redirect()->to('login')->withErrors('auth.failed');
    }

    //create a session
    $user = Auth::getProvider()->retrieveByCredentials($request->only($field, 'password'));
    Auth::login($user);

    return redirect()->to('/');
}

don't forget to configure your web.php file, so that the routes work for you. I hope I've helped.

  • Related