Introduction

When building modern APIs or form-driven applications, Laravel validation gives you elegant tools to keep your data clean and consistent. But things get trickier with partial updates, where users may only update one or two fields. That’s where Laravel’s conditional helpers — sometimes() and whenFilled() — shine. In this guide, we’ll break down how each works, when to use them, and how to combine both for flexible and powerful validation logic.

When building APIs or web forms, conditional validation becomes crucial — especially when handling partial updates (like PATCH requests).

In Laravel, two methods often come up for this: sometimes() and whenFilled().
At first glance, they sound similar — but they serve different purposes.

Let’s explore both in depth with practical examples.


The Problem: Partial Updates

Imagine you have an endpoint to update a user profile:

PATCH /api/users/{id}

You want users to be able to update only the fields they provide — without forcing them to resend everything. For example, they might just want to update their name or password, not both.


The sometimes() Method

The sometimes() method lets you apply rules only under certain conditions.

How It Works

When you define your validator, you can specify that a rule should apply “sometimes” — depending on a condition you provide as a closure.

Example:

$validator = Validator::make($data, [
    'email' => 'email',
    'password' => 'min:8',
]);

$validator->sometimes('password', 'required', function ($input) {
    return isset($input->email);
});

What happens here:

  • The password field is only required if email is present in the input.
  • If the user doesn’t send email, Laravel won’t complain about the missing password.

Perfect for conditional validation — like “only require this field when another one is provided.”


The whenFilled() Method

whenFilled() is a newer and more expressive approach introduced in Laravel 9.

How It Works

This method runs your callback only if the field is both present and not empty.

In other words:

“If this input is filled, then do something.”

Example in a Form Request

public function passedValidation()
{
$this->whenFilled('password', function ($value) {
$this->merge(['password' => bcrypt($value)]);
});
}

What happens here:

  • If the password field is filled (not null or empty), it will be automatically hashed.
  • If it’s missing or empty, Laravel skips this logic entirely.

Using Both in a Partial Update (PATCH) Request

Here’s a practical example combining the two in a User Update Form Request:

class UpdateUserRequest extends FormRequest
{
    public function rules(): array
    {
        return [
            'name' => ['sometimes', 'string'],
            'email' => ['sometimes', 'email', 'unique:users,email,' . $this->user->id],
            'password' => ['sometimes', 'string', 'min:8'],
        ];
    }

    public function withValidator($validator)
    {
        // Only apply confirmation if password is present
        $validator->sometimes('password', 'confirmed', function ($input) {
            return !empty($input->password);
        });
    }

    public function passedValidation()
    {
        // Automatically hash password if filled
        $this->whenFilled('password', function ($value) {
            $this->merge(['password' => bcrypt($value)]);
        });
    }
}

What’s Happening:

  1. sometimes() ensures fields are only validated if they exist in the request.
  2. The sometimes(..., 'confirmed', ...) rule applies password confirmation only when password is sent.
  3. whenFilled() ensures password hashing only happens if the user provided one.

This combination makes the form robust and flexible — ideal for PATCH routes where only some fields are being updated.

Key Differences

Featuresometimes()whenFilled()
PurposeAdd rules conditionallyPerform actions conditionally
Runs when…A custom condition (closure) is trueThe field is present and not empty
ScopeValidator logicForm Request or manual callback
Typical use case“Validate this only if…”“Do this only if field is filled”
Good forConditional validation rulesConditional data manipulation

Quick Tip: Combine with filled Rule

Sometimes you just want to validate a field only if it’s filled.
Laravel’s built-in filled rule can simplify this too:

'password' => ['filled', 'string', 'min:8'],

That tells Laravel:

“Validate this rule only when the field is filled.”

You might still use whenFilled() for extra behavior, like hashing or transforming input.

Conclusion

Both sometimes() and whenFilled() give you fine-grained control over conditional validation in Laravel:

  • Use sometimes() to apply rules dynamically.
  • Use whenFilled() to perform actions (like hashing) only when data is present.

Together, they make your validation logic cleaner, smarter, and perfectly suited for partial updates.

Categorized in: