I'm currently available for full time hire! Inquire Here

This post is more than a year old. There is a chance the content is outdated.

Nullable vs. Sometimes

Nullable vs. Sometimes


Often times you have validation in your requests using one or more of the wide variety of rules that come built in with Laravel.

The more fields you are validating, the more chance you don't actually need all of them to process what you're doing.

There are quite a few validation rules to reach from to exclude validation, but when I have data that is not required, I usually reach for one of these:

Nullable

Using the nullable rule, you are telling Laravel that the field under validation may be null but if it is not null, to validate it against the rest of rules in the array for that key.

This is useful when paired with the TrimStrings and ConvertEmptyStringsToNull middleware, as any field sent through the request will be trimmed, and if that string is empty, will be converted to null. So you can safely send through empty strings and expect null to validate on the other end.

For example:

1request()->validate([
2 'first_name' => ['required', 'string'],
3 'middle_initial' => ['nullable', 'string', 'max:1'],
4 'last_name' => ['required', 'string'],
5]);

In this example, assume 3 text inputs on the UI, for first_name, middle_initial, and last_name. Also assume no HTML5 or Javascript validation is being applied on the frontend.

If the user were to leave first_name or last_name blank, they would ge a validation error. However, if they were to leave middle_initial blank, the ConvertEmptyStringsToNull middleware would take it and convert it to null, and the nullable validation rule would be applied which would skip the rest of the rules.

However, if the user enters anything in the field, it will be validated against the rest of the rules.

Sometimes

The sometimes rule is slightly different but instead of validating if the field is null, it validates if the field is present in the current request at all.

A good example is when you have a form that is maybe interacting with javascript, or run by a javascript framework. You may be enabling/disabling fields on that form based on the user's interaction.

The sometimes validation rule comes into play here, as any disabled field will not be present in the request.

For example:

1request()->validate([
2 'first_name' => ['required', 'string'],
3 'last_name' => ['required', 'string'],
4 'phone_number' => ['sometimes', 'required', 'digits:10'],
5]);

In this example, if there's a part of the form that disables the phone_number input, then the digits validation rule will not be applied. However, if the field is not disabled, and the form sends through anything else, even and empty string that gets converted to null, than the validation rule will get fired against the rest of the rules.

The takeaway from this article is that if the field you are validating has to exist, but can be null, use nullable. If the field you are validating may or may not be present in the request, and you need to validate it when it is, use sometimes.

Anthony Rappa

By Anthony Rappa

Hello! I'm a full stack developer from Long Island, New York. Working mainly with Laravel, Tailwind, Livewire, and Alpine.js (TALL Stack). I share everything I know about these tools and more, as well as any useful resources I find from the community. You can find me on GitHub and LinkedIn.