Framework: [5.4] Validation Rule nullable with required failed (not expected)

Created on 30 Mar 2017  路  9Comments  路  Source: laravel/framework

  • Laravel Version: 5.4.16
  • PHP Version: 7.0.15
  • Database Driver & Version: -

Description:

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.

Steps To Reproduce:

$rules = ['id' => 'required|nullable'];
$data = ['id' => null];
$this->validate($data, $rules);

in unit test or in command line (tinker)

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.

All 9 comments

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.

Why I think this decision not logical

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.

The suggested alternative

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);

Workaround

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 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 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.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

klimentLambevski picture klimentLambevski  路  3Comments

kerbylav picture kerbylav  路  3Comments

SachinAgarwal1337 picture SachinAgarwal1337  路  3Comments

digirew picture digirew  路  3Comments

ghost picture ghost  路  3Comments