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

Published 1 week 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?

Best Answer (As Selected By procell)
procell

An update: there was a completed unrelated issue, everything works as I expect it to. The response data I was dd()ing in the test is long, and I was seeing the custom message for the after:now rule, but the actual failed validation rule was a bit further up in the response data.

tldr pebcak

procell
procell
1 week ago (12,680 XP)

An update: there was a completed unrelated issue, everything works as I expect it to. The response data I was dd()ing in the test is long, and I was seeing the custom message for the after:now rule, but the actual failed validation rule was a bit further up in the response data.

tldr pebcak

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