Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

LaraBABA's avatar

Laravel rules issues

Hi all,

I have 2 very identical forms and would like to use the same rules in a request. My request file currently has:

// \App\Http\Requests\UserStoreRequest
    public function rules()
    {
        return [
            'first_name' => ['required', 'max:255', 'string'],
            'last_name' => ['required', 'max:255', 'string'],
            'email' => ['required', 'unique:users,email', 'email'],
            'password' => ['required'],
            'business_name' => ['nullable', 'max:255', 'string'],
            'roles' => 'array',
        ];
    }

I have 2 store methods, one of them for a full form and the other for a fast filling of the same form. The only difference is that the fast form needs to auto generate a password. I was hoping to use the same request file(with rules) for both forms by bypassing the password with a "nullable" rule. To my surprise, the password is still required. Any idea how to bypass the password policy from "UserStoreRequest"?


    public function storeFast(UserStoreRequest $request)
    {
        $validated = Validator::make(array_merge($request->all(), $request->route()->parameters()), [
            'password' => 'string|max:64',
        ]);

        $validated['password'] = Hash::make(str_random(12));
        $validated = $request->validated();

Thanks

0 likes
6 replies
Niush's avatar
Niush
Best Answer
Level 50

Inside rules, if current route is fast form, mark as nullable. Else require it.

//...
'password' => ( request()->route()->getName() === 'user.store.fast' 
                ? ['nullable'] 
                : ['required'] ),
//...
LaraBABA's avatar

@Niush Thank you so much. I did not think about this. I am not managing to get it to work at the moment as I am getting a 302 error but I will keep playing with it as your solution seems the most logical one.

LaraBABA's avatar

@Niush I tried this:

'password' => ( request()->route()->getName() === 'user.store.fast' 
                ? ['nullable'] 
                : ['required'] ),

But the validation does not pass. The right controller and method is hit:

Method	POST
Controller Action	App\Http\Controllers\UserController@storeFast
Middleware	web, auth
Path	/users/store/fast
Status	302
Duration	282 ms
IP Address	127.0.0.1
Memory usage	22 MB

I get no errors whatesoever, the form just reloads, as soon as I replace your code with the ['required'] I get the required error but can pass the validation if I input a password. So there is something wrong.

I also logged "request()->route()->getName()" and correctly receive 'user.store.fast'

Niush's avatar

@User476820 If there is no error response, then it is working I guess. May be the controller is reached, and is redirecting back or doing something?

LaraBABA's avatar

@Niush If I put your code I never reach the controller (var_dump() at the request level), when I remove it, the controller kicks in and the data is saved. Still trying to figure out why.

Please or to participate in this conversation.