Laravel Validation If checkbox ticked then Input text is required?
Stefan Bogdanescu
Founder & Senior Architect · 2026-06-29
Laravel Validation: Implementing Conditional Requirements with Checkboxes
As developers working with Laravel, one of the most common challenges we face is implementing conditional validation. This occurs when the requirement for one field depends on the value or state of another field—in your case, if a checkbox is ticked, another text input must be mandatory. Trying to force this logic purely within the basic $rules() array can become cumbersome.
This post will walk you through the most effective and idiomatic ways to achieve this conditional requirement in Laravel, moving beyond simple required checks to implement true dependency logic.
Understanding the Challenge: Conditional Dependencies
Your scenario is:
- If
has_logincheckbox is checked (value is present/true), thenpinmust be required. - If
has_loginis not checked,pincan be empty.
A simple rule set like ['has_login' => 'accepted', 'pin' => 'required'] fails because it enforces the pin requirement regardless of the checkbox state. We need dynamic logic to link these two fields together within the validation process.
Solution 1: Using the sometimes Rule (The Simplest Approach)
For scenarios where a field should only be validated if another condition is met, the sometimes rule is often the cleanest solution in Laravel validation. It tells the validator: "Only apply this rule if the field exists or meets certain criteria."
While sometimes isn't a direct 'if/then' statement based on another field's value, we can leverage it by setting up rules that depend on the presence of data. However, for true cross-field dependency, we often need slightly more advanced techniques.
Solution 2: Implementing Custom Validation Logic (The Robust Approach)
For complex dependencies like this, where Field A dictates the requirement for Field B, the most robust method is to write custom validation logic within your Request or Controller. This gives you explicit control over the relationship between the data points.
We will use a closure within the $rules() method to inspect both fields simultaneously before deciding on the validation outcome.
Here is how you can structure this in a Laravel Request:
use Illuminate\Http\Request;
class LoginFormRequest extends Request
{
public function rules()
{
// Define base rules for all fields
$rules = [
'has_login' => 'sometimes|boolean', // Check if the checkbox exists and is a boolean
'pin' => 'nullable', // Start by allowing it to be empty
];
// Add custom conditional logic using a closure
$rules['pin'] = function ($attribute, $value, $fail) {
// Check if 'has_login' was accepted (i.e., is true or 1)
if (request('has_login') == 1) {
// If the checkbox is ticked, then 'pin' MUST be present
if (empty($value)) {
$fail('The PIN is required when login status is selected.');
}
}
// If the condition is not met, validation passes for this field.
};
return $rules;
}
}
Explanation of the Logic:
- Base Rules: We set
pintonullableinitially. This means if no other rule triggers a failure, an empty value is acceptable. - The Closure: We override the rule for
pinwith a closure. This allows us to access data from the entire request (request('has_login')). - Conditional Check: Inside the closure, we check if
request('has_login')equals1(assuming your checkbox sends a value of 1 when checked). If it is true, we then explicitly check if$value(the submitted PIN) is empty. If it is empty, we trigger the validation failure using$fail().
This approach ensures that the requirement for the pin field is dynamically determined by the state of the has_login checkbox, making your validation logic explicit and highly flexible.
Conclusion
Implementing conditional validation in Laravel requires moving beyond simple, static rule definitions. While basic rules are excellent for standard requirements, complex dependencies necessitate leveraging custom closures within your validation setup. By understanding how to inspect multiple input values before applying a rule—as demonstrated with the request() helper and closure logic—you gain the power to build highly contextual and user-friendly forms. For deeper insights into structuring large applications, exploring patterns found in high-quality frameworks like Laravel Company is always recommended.