4jZW7jVSdS4U6PC's avatar

FormRequest method to ignore null field upon validation in L5.4

Hello everyone.

I've a Form Request with this rule:

"password" => "confirmed"

This solution works well in L5.3 because the rule itself has a bug, if you pass two empty fields like

"password" => ""
"password_confirmation" => ""

they will be validated because both are empty strings (""). Even though this is a bug, I guess, it's fine in a certain degree because my application does other checks after the request anyway but in L5.4 every empty fields are converted to null and the "confirmed" rule alone does not work anymore because of the new middleware.

I tried

"password" => "sometimes|confirmed"

but it does not work because the key exists.

My question is simple:

Is there a validation rule that works similar to sometimes but it checks nullable values instead?

I would like to keep the middleware though.

0 likes
4 replies
4jZW7jVSdS4U6PC's avatar

@jekinney interesting but the description doesn't help me.

Should I use it like this?

"password" => "required_unless:password_confirmation, null|confirmed"
1 like
jekinney's avatar
jekinney
Best Answer
Level 47

@ludo237

Almost. Never use a space in validation string. The value, if empty, means any value just not null or empty. This "should work" (my fingers are crossed because I do also want to know).

"password" => "required_if:password_confirmation|confirmed"
1 like
4jZW7jVSdS4U6PC's avatar

AFAIK with some "live" testing this seems to work!

I will write some dedicated tests tomorrow. Thank you.

Please or to participate in this conversation.