When I started using Laravel 12, validation was one of the first things I had to master. Especially conditional rules like `required_if` and `nullable` made my forms smarter and more flexible.
In this guide, I'll show you how to use these validation rules easily. We'll go through clear examples so you can use them right away in your Laravel projects.
Let's dive into Laravel 12 Validation with some simple code!
$request->validate([
'company_name' => 'required_if:is_employed,yes|nullable',
]);
Validation in Laravel 12 is the process of making sure the data coming into your application is clean, correct, and secure before using it. For example, when users fill out a form, we need to check if they entered the required information, like a valid email address or a minimum password length.
Laravel 12 makes validation very simple and elegant. It provides powerful methods like $request->validate()
that allow us to quickly apply validation rules without writing long, complicated code.
This not only protects our applications from bad data but also improves user experience by giving instant feedback when something is wrong.
Simple Example:
$request->validate([
'email' => 'required|email',
'password' => 'required|min:8',
]);
Conditional validation in Laravel 12 means applying validation rules based on the value of another input field. Sometimes, a field should only be required if a certain condition is true.
For example, if a user says they are employed, then we might require them to fill in their company name. If they are not employed, the company name can stay empty.
Instead of writing custom logic, Laravel lets us handle this easily using rules like required_if
, required_unless
, and others.
Example:
$request->validate([
'company_name' => 'required_if:is_employed,yes',
]);
Example:
$request->validate([
'middle_name' => 'nullable|string|max:255',
]);
$request->validate([
'company_name' => 'required_if:is_employed,yes|nullable|string|max:255',
]);
If condition matches, it's required; otherwise, it can be empty.
Example of required_unless:
The field must be present unless another field has a certain value
$request->validate([
'company_name' => 'required_unless:is_employed,no',
]);
If is_employed
is not "no", then company_name
is required.
If is_employed
is "no", company_name
can be empty.
Good for when the exception happens based on a specific value.
Example of required_with:
The field is required only if another specified field is present (not empty).
$request->validate([
'state' => 'required_with:country',
]);
If country
is filled in, then state
must also be filled.
If country
is not provided, state
can be empty.
Useful when fields depend on each other (like country → state)
Example of required_without:
The field is required if another specified field is NOT present (empty).
$request->validate([
'email' => 'required_without:phone',
]);
If phone
is missing, email
becomes required.
If phone
is filled, email
can be empty.
Good when you want to accept either of two fields (email or phone).
Example of sometimes:
$request->validate([
'phone' => 'sometimes|required|numeric',
]);
Validate only if phone
field is present
Conditional validation in Laravel 12 makes our apps smarter and more user-friendly. By using simple rules like `required_if` and `nullable`, we can easily handle different scenarios without writing complicated logic. Keep practicing these techniques and you'll master Laravel validation in no time!
Use the required_if
rule:
'company_name' => 'required_if:is_employed,yes'
It allows a field to be empty without failing validation.
Yes! Example:
'company_name' => 'required_if:is_employed,yes|nullable|string'
nullable: Field can be null.
sometimes: Validation rules are applied only if the field exists in the input.
For more detailed information, refer to the official Laravel documentation on validation.
You might also like: