Laravel Validation of Required With

Stefan Bogdanescu

Founder & Senior Architect · 2026-06-29

Laravel Company

Mastering Cross-Field Dependencies: The Laravel Way to Validate Related Data

As developers working with web applications, one of the most common challenges we face is managing complex data dependencies during form submission. When fields depend on each other—like an address requiring a city and state—setting up robust validation can feel like navigating a maze. If you are encountering issues where dependent fields don't trigger their required checks correctly, it usually points to a misunderstanding of how Laravel’s validation rules interact, especially when dealing with relational requirements.

This post will walk you through the correct, robust way to handle these cascading dependencies using Laravel's built-in validation features, ensuring your data integrity remains impeccable.

Understanding required_with_all in Depth

The primary tool for enforcing that multiple fields must be present simultaneously is the required_with_all rule. This rule instructs the validator that if any of the specified fields are present, all other listed fields must also be present and non-empty.

Let's analyze the structure you provided:

'address' => 'required_with_all:city,state,zip|string|nullable',
'address2' => 'required_with:address|string|nullable',
'city' => 'required_with_all:address,state,zip|string|nullable',
// ... and so on

The confusion often arises because required_with_all operates based on the presence of data in the request. If you submit only city, Laravel checks if city is required (it is) and then checks if all fields listed in :address,state,zip are also present. If you only submitted city, the validator sees that address and state are missing, and thus it correctly fails the validation for city.

The key to success here is ensuring your base field (address) is consistent across all dependent rules.

Solving the Dependency Chain: A Practical Example

To make these dependencies work seamlessly, we need a clear hierarchy where the most fundamental piece of data drives the subsequent requirements.

Consider setting up validation for an address block:

// Example Validation Rules in a Form Request or Controller
$rules = [
    'address' => 'required|string', // Base requirement
    'city' => 'required|string',
    'state' => 'required|string|size:2',
    'zip' => 'required|integer|digits:5',
];

// Applying the cross-field requirements:
$rules['address'] = ['required', 'string']; // Base field must exist.
$rules['city'] = ['required', 'string', 'required_with:address'];
$rules['state'] = ['required', 'string', 'size:2', 'required_with:address,city'];
$rules['zip'] = ['required', 'integer', 'digits:5', 'required_with:address,city,state'];

Notice how we chain the dependencies. For instance, state requires both address AND city. This creates a logical flow that mirrors real-world data requirements perfectly. When you submit only city, the validation for state will fail because address is missing, which is exactly the desired behavior.

Best Practices: Using Form Requests for Cleanliness

While defining these rules directly in a controller is possible, the most idiomatic and maintainable approach in modern Laravel development is to encapsulate all this logic within a dedicated Form Request class. This keeps your controller clean and adheres to the principles of separation of concerns, which is a core tenet of good application design, much like the patterns discussed by the team at https://laravelcompany.com.

By using Form Requests, you centralize validation rules, making them reusable, testable, and easier to manage as your application grows. This structured approach prevents the kind of subtle dependency errors that plague complex custom logic in controllers.

Conclusion

Setting up dependent validation with required_with_all is entirely achievable, but it requires a disciplined approach to defining the dependency hierarchy. Don't rely on one field alone; build relational rules by chaining them logically based on your data model. By structuring your dependencies clearly and leveraging tools like Form Requests, you ensure that your application enforces strict data integrity from the very first submission.