Laravel Error to inicialice ValidationFactory

Viewed 23

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 class="vh-100">
  <div class="container-fluid h-custom">
    <div class="row d-flex justify-content-center align-items-center h-100">
      <div class="col-4">
          <img src="{{ URL::asset('img/logo.png') }}"
          class="img-fluid" height="500">
      </div>
      <div class="col-md-8 col-lg-6 col-xl-4 offset-xl-1">
        <form action="{{route('samein.login')}}" method="POST">
          @csrf
          <!-- Email input -->
          <div class="form-outline mb-4">
            <input type="text" name="username" class="form-control form-control-lg"
              placeholder="Usuario" />
            <label class="form-label" for="form3Example3">Usuario</label>
          </div>

          <!-- Password input -->
          <div class="form-outline mb-3">
            <input type="password" name="password" class="form-control form-control-lg"
              placeholder="Contraseña" />
            <label class="form-label" for="form3Example4">Contraseña</label>
          </div>

          <div class="text-rigth text-lg-start mt-4 pt-2">
            <button type="submit" class="btn btn-primary btn-lg"
              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();
    }
}

0 Answers
Related