When working with Laravel's validation system, it's important to understand the difference between the present
and required
rules. While they might seem similar at first glance, they serve distinct purposes.
required
The required
rule ensures that a field exists in the request and is not empty. Itβs one of the most common validation rules and is typically used to enforce that users provide a value.
$request->validate([
'email' => 'required|email',
]);
In this example, if the email
key is missing or its value is empty (e.g., null
, empty string), validation will fail.
present
The present
rule only checks that the field exists in the input, regardless of its value. It does not require the field to have a non-empty value.
$request->validate([
'token' => 'present',
]);
This is useful when you expect a key to be present β such as a checkbox that may submit a falsy value (0
, false
, or an empty string) β but donβt want to enforce that it has a value.
Example
Imagine a form with a checkbox named subscribe
:
- If you use
required
, the checkbox must be checked. - If you use
present
, the checkbox input must be sent, but its value can be anything (even unchecked).
Summary
Rule | Requires key to be present? | Requires value to be non-empty? |
---|---|---|
present |
β Yes | β No |
required |
β Yes | β Yes |
Choose present
when you care about the existence of a field, and required
when you also need a value.
If this post was enjoyable or useful for you, please share it! If you have comments, questions, or feedback, you can email my personal email. To get new posts, subscribe use the RSS feed.