In Laravel 5.4.15 the following test is valid:
$rules = ['id' => 'required|nullable'];
$data = ['id' => null];
$this->validate($data, $rules);
In Laravel 5.4.16 the same code is invalid - so all my unit tests failed. I think this is not the correct behaviour, because the key id
is in the data and the ruleset needs a key id
and allows being null - that it is.
$rules = ['id' => 'required|nullable'];
$data = ['id' => null];
$this->validate($data, $rules);
in unit test or in command line (tinker)
I have no middleware for changing the values of a request or something like that
In a recent change we made null
invalid in a required
rule, the old behaviour was reported multiple times as a bug, when you say a field is required then it doesn't really make sense to accept null
as a value for that field. May I ask what's the use case?
In my use case I have to have a field in the data with a key. But null is a valid value, like an integer too.
Sorry but we had to change this since people reporting it as a bug was happening on daily basis :) My suggestion is that you filter the data before passing to the validator and remove the nullable fields.
Ok, but technically I think your decision is wrong. I will change my code.
@themsaid I do not agree that this decision is the most logical.
I have an API endpoint that only accepts one attribute and the value can be null.
I want the validation errors to guide the users to know which attribute is available.
request()->validate([
'data' => 'required|nullable',
]);
I think this scenario should throw a validation error if the key data
does not exists. This will force the use of a key named data
, but the value chould be null
.
Removing the required
rule as suggested, will not pick up up if the user is using the wrong attribute.
$this->post('store', [
'not-right-attribute'=>'my value',
])->assertStatus(201);
This is a workaround, but it feels dirty.
if (! request()->has('data')) {
request()->validate([
'data' => 'required',
]);
}
request()->validate([
'data' => 'nullable',
]);
@LasseHaslev I am having this same issue. However, I discovered that there is a present
validation rule that checks the field exists in the input data, but can be empty.
@brandonferens Thank you!
Changed my code from this
if (! request()->has('data')) {
request()->validate([
'data' => 'required',
]);
}
request()->validate([
'data' => 'nullable',
]);
To this
request()->validate([
'data' => 'present',
]);
And all my test are still working! 馃憤
In a recent change we made
null
invalid in arequired
rule, the old behaviour was reported multiple times as a bug, when you say a field is required then it doesn't really make sense to acceptnull
as a value for that field. May I ask what's the use case?
In a case of nullifying required nullable field, in other words updating its value to NULL, it is very useful to have option to have both rules available in combination. I wonder in which case these two rules look like a bug. Combined they clearly say: "Field must have value or be null". Null is a value too, unknown, but a value. To me, it sounds much more like a bug to specify that a field is nullable, and have that ignored.
There are workarounds ofc, but this looks like a no brainer to me.
Most helpful comment
@LasseHaslev I am having this same issue. However, I discovered that there is a
present
validation rule that checks the field exists in the input data, but can be empty.