Laravel validation rules - optional, but validated if present

Stefan Izdrail

Founder & Senior Architect · 2026-06-29

Laravel Company
Title: Laravel Validation Rules - Optional But Validated If Present Introduction: Laravel is an incredible framework for developing web applications, offering various features that make the development process easier for developers. One such feature is validation, which ensures that user input meets specific requirements before executing a particular action. In this article, we will discuss how to create Laravel form validations where the rule applies only if the field is present, but not null or empty. This can be useful in situations where you want to allow users to leave certain fields blank or not alter them at all during an update process. Body: Creating Validation Rules for Optional Fields with Value To achieve this goal, we will begin by creating a controller to handle the form submissions and include validation rules. Here is a basic example of a controller method that accepts a request and validates the input:
public function update(Request $request) {
    $validator = Validator::make($request->all(), [
        'test' => 'sometimes|nullable|min:6',
    ]);
    if ($validator->fails()) {
        return back()->withErrors($validator)->withInput();
    }

    // Save the updated user data to database ...
}
In this example, we created a single validation rule for the 'test' field that uses the 'sometimes', 'nullable', and 'min:6' validators. The 'sometimes' validation makes sure that the test will only be validated if it is passed as not null. The 'nullable' validator allows the field to be empty, and the 'min:6' validator checks for a minimum length of 6 characters. To make this more practical, let's assume that we have two fields: password_current (optional) and password_new (required). We will update the validation rule as follows:
public function update(Request $request) {
    $validator = Validator::make($request->all(), [
        'password_current' => 'nullable|sometimes',
        'password_new' => 'required|min:6',
    ]);
    if ($validator->fails()) {
        return back()->withErrors($validator)->withInput();
    }

    // Save the updated user data to database ...
}
In this case, the password_current field can either be blank or contain a valid value. It will only be validated if it is passed as not null, while the password_new field is always required and must meet the minimum length requirement. Conclusion: By understanding and correctly applying Laravel validation rules for optional fields with value, you can make your forms more user-friendly and maintain data integrity. This article aimed to provide a comprehensive answer on how to create Laravel form validations where the rule applies only if the field is present but not null or empty. For further reading or support on Laravel development, visit our website at https://laravelcompany.com for valuable resources and expert guidance.