Testing controller, null value passed to store method has nullable validation rule but still fails

Posted 4 months ago by procell

I've seen a couple threads that ask much the same question, and I've tried the solutions for those issues, but I'm still running into a problem.

I'm testing the store method for a controller by posting an array of data to it. I pass it null for a scheduled_for array key in the test. The store method on the controller accepts a StoreModelRequest Form Request, and inside that, I have a rules array that declares scheduled_for => 'nullable|date|after:now rules.

When I'm testing, this rule is failing with the custom message You need to schedule this for a future date and time. When I'm submitting this form via the application's UI and pass scheduled_for a value of null, the after:now rule passes, as I expect it to.

I have ->nullable() in the model's table schema for the scheduled_for column. I have tried to use the sometimes validation rule alongside the nullable validation rule inside the StoreModelRequest Form Request, and this doesn't make a difference.

I'm using Laravel 5.6.

What could be causing this?

Please sign in or create an account to participate in this conversation.

Laracasts Mascot

Hi, Have We Met Yet?

Did you know that, in addition to the forum, Laracasts includes well over 1000 lessons on modern web development? All for the price of one lunch out per month.

Sign Me Up

Channels

Reply to

Use Markdown with GitHub-flavored code blocks.