Laravel validation: validate field only if another is present
Stefan Bogdanescu
Founder & Senior Architect · 2026-06-29
Laravel Validation: Validating Fields Conditionally – The Right Way
Dealing with conditional validation in Laravel often trips up developers. We frequently need rules that depend on the presence or value of other fields in the same request, moving beyond simple required checks. The scenario you described—validating account_id only when needs_login is present—is a classic example of this complexity.
Let’s break down why your initial attempts with required_with and sometimes fell short and establish the most robust pattern for conditional validation in Laravel.
The Frustration: Why Simple Rules Don't Always Cut It
You started with:
$rules = [
'account_id' => ['required_with:needs_login', 'custom_validation']
];
And then tried adding 'sometimes':
$rules = [
'account_id' => ['required_with:needs_login', 'sometimes', 'custom_validation']
];
The issue stems from the difference between requiring a field based on another's existence, and simply conditionally running a validation rule.
required_with:needs_login: This rule is designed to enforce that ifneeds_loginis present, thenaccount_idmust also be present. Ifneeds_loginis missing entirely from the request, this rule doesn't trigger any requirement onaccount_id. However, it doesn't solve the scenario where you want to allowaccount_idto exist independently, but only enforce its presence when the condition is met.sometimes: This modifier tells Laravel: "If this field exists in the request data, then run the following rules on it." Ifneeds_loginis absent,account_idmight still be present, and the rule simply checks ifaccount_idexists before runningcustom_validation, which isn't the specific dependency logic you need.
We need a method that strictly links the presence of one field to the requirement of another, regardless of whether the dependent field is actually submitted.
The Correct Approach: Conditional Logic and Form Requests
For complex conditional dependencies like this, relying purely on array rules can become cumbersome. A more structured approach involves leveraging Laravel's powerful Form Request system or custom logic within your controller/service layer to handle these prerequisites before validation even occurs.
However, if you must manage this entirely within the validation layer, we need a way to dynamically adjust the rules based on input—which often points toward using closures or conditional rule definitions.
Solution 1: Dynamic Rule Injection (The Advanced Method)
Since standard built-in rules struggle with true "if X then Y is required" logic when X is optional, the most reliable method involves checking the presence of the dependency and dynamically adding or removing rules based on that check before validation runs.
If you are using a Form Request, you can perform this conditional logic directly within the rules() method:
public function rules()
{
$rules = [
'needs_login' => 'required', // Assume this field is always checked for presence
'account_id' => 'nullable', // Start by allowing it to be absent initially
];
// Check if the dependency exists in the request data
if ($this->has('needs_login')) {
// If needs_login is present, then account_id MUST be present and required.
$rules['account_id'] = ['required', 'numeric'];
} else {
// If needs_login is NOT present, we simply allow account_id to be optional (nullable).
$rules['account_id'] = ['nullable', 'numeric'];
}
return $rules;
}
This approach ensures that the validation rules are context-aware. It forces the system to evaluate the dependency before deciding whether account_id needs to be strictly required or merely optional. This pattern demonstrates how powerful Laravel is for building highly contextual data processing, much like the robust structure seen in frameworks like those provided by laravelcompany.com.
Conclusion
Avoid trying to force complex conditional requirements into simple modifiers like sometimes. Instead, when you need true "if-then" dependencies between fields, shift your focus to dynamic rule generation within your validation layer, often facilitated by checking the input data explicitly. This keeps your validation logic clean, explicit, and highly predictable, ensuring that your application enforces exactly the business rules you intend.