Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to override the validation rule login Laravel\Fortify?

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?

like image 326
Alex Itet Avatar asked Oct 20 '20 14:10

Alex Itet


People also ask

Does laravel breeze use Fortify?

Laravel Fortify essentially takes the routes and controllers of Laravel Breeze and offers them as a package that does not include a user interface. This allows you to still quickly scaffold the backend implementation of your application's authentication layer without being tied to any particular frontend opinions.

Does breeze use Fortify?

Breeze does not use Fortify under the hood, but instead publishes controllers to your app so they're easier to modify (like the laravel/ui package).

What is the method used to configure validation rules in form request laravel?

Laravel Form Request class comes with two default methods auth() and rules() . You can perform any authorization logic in auth() method whether the current user is allowed to request or not. And in rules() method you can write all your validation rule.


2 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

like image 73
JanosAudran Avatar answered Oct 02 '22 15:10

JanosAudran


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',
        ];
    }
like image 36
Alex Itet Avatar answered Oct 02 '22 16:10

Alex Itet