How to override the validation rule login Laravel\Fortify?

Viewed 7037

This described in the class Laravel\Fortify\Http\Requests\LoginRequest
I want to add one more validation line

namespace Laravel\Fortify\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;
use Laravel\Fortify\Fortify;

class LoginRequest extends FormRequest
{
/..
    public function rules()
        {
            return [
                Fortify::username() => 'required|string',
                'password' => 'required|string',
            ];
        }
}

But I cannot do the customizations in the vendor.
My question is how to do this?

3 Answers

Just to add to the comment above, it's a bad idea to make changes in the vendor folder as stated earlier. For one, any code pushes to a repository will not reflect these changes (unless you modify the ignore file).

For Laravel/Fortify adding new fields and changing the default validation rules, even the password requirements is very straightforward. It's not clear to me what your requirement is, but it might be easier to simply use a Validator. That is what Fortify uses as well. For example, Fortify publishes two files:

App\Actions\Fortify\CreateNewUser.php
App\Actions\Fortify\PasswordValidationRules.php

To add a new validation rule for a field, simply add it the CreateNewUser.php under the Validator::make method that Fortify itself is using. You can follow the same logic in your custom implementation. For example to add a firstname field, modify as follows:

Validator::make($input, [
            'firstname' => ['required', 'string', 'max:255'],
        ])->validate();

        return User::create([
            'firstname' => $input['firstname'],
        ]);

You can add as many field as you want. To change the password requirements, make changes to the passwordRules() function in the PasswordValidationRules.php file as follows:

protected function passwordRules()
        {
        return ['required', 
        'string', 
        (new Password)->requireUppercase()
        ->length(10)
        ->requireNumeric()
        ->requireSpecialCharacter(), 
        'confirmed'];
    }

All this info can be found at the official docs https://jetstream.laravel.com/1.x/features/authentication.html#password-validation-rules

In short, I solved the problem like this

copy vendor\laravel\fortify\src\Http\Controllers\AuthenticatedSessionController.php to app\Http\Controllers\Auth\LoginController.php (change namespace and class name)

copy vendor\laravel\fortify\src\Http\Requests\LoginRequest.php to app\Http\Requests\LoginFormRequest.php (change namespace and class name)

add new route in routes/web.php

use App\Http\Controllers\Auth\LoginController;
//
Route::post('/login', [LoginController::class, 'store'])
        ->middleware(array_filter([
            'guest',
            $limiter ? 'throttle:'.$limiter : null,
        ]));

in LoginController changed LoginRequest to LoginFormRequest and

public function store(LoginFormRequest $request)
    {
        return $this->loginPipeline($request)->then(function ($request) {
            return app(LoginResponse::class);
        });
    }

in LoginFormRequest add my new rule(s)

public function rules()
    {
        return [
            Fortify::username() => 'required|string',
            'password' => 'required|string',
            'myNewRule' => 'required|string',
        ];
    }
Related