How to validate aninput field if the value is not NULL in Laravel?

In Laravel, you can validate input fields to ensure they are not NULL using the Validation class. The required rule is the most common way to validate that a field is not null or empty.

Basic Validation Syntax

The Validator::make() method takes two arguments the data to validate and the validation rules ?

<?php
$validator = Validator::make(
    array('name' => 'Disha'),
    array('name' => 'required|min:5')
);
?>

In this example, the name field is required and must have at least 5 characters.

Complete Validation Example

Here's a complete controller example that validates multiple fields for non-null values ?

<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;
use Illuminate\Routing\Router;
use Illuminate\Validation\Rule;

class TestUserInput extends Controller {
    public function index() {
        $formData = array(
            'firstname' => 'Siya',
            'lastname' => 'Nadkarni',
            'address' => 'xyz'
        );
        
        $rules = array(
            'firstname' => 'required|string',
            'lastname' => 'required|string',
            'address' => 'required|string'
        );
        
        $validator = Validator::make($formData, $rules);
        
        if ($validator->fails()) {
            echo "Validation Failed";
        } else {
            echo "Validation Successful";
        }
    }
}
?>
Validation Successful

Missing Field Validation

When a required field is missing from the input data, validation fails ?

<?php
$formData = array(
    'lastname' => 'Nadkarni',
    'address' => 'xyz'
    // firstname is missing
);

$rules = array(
    'firstname' => 'required|string',
    'lastname' => 'required|string',
    'address' => 'required|string'
);

$validator = Validator::make($formData, $rules);

if ($validator->fails()) {
    echo "Validation Failed";
} else {
    echo "Validation Successful";
}
?>
Validation Failed

NULL Value Validation

When a field contains a NULL value, the required rule will catch it and fail validation ?

<?php
$formData = array(
    'firstname' => null,
    'lastname' => 'Nadkarni',
    'address' => 'xyz'
);

$rules = array(
    'firstname' => 'required|string',
    'lastname' => 'required|string',
    'address' => 'required|string'
);

$validator = Validator::make($formData, $rules);

if ($validator->fails()) {
    echo "Validation Failed";
} else {
    echo "Validation Successful";
}
?>
Validation Failed

Common Validation Rules for Non-NULL Fields

Rule Purpose Example
required Field must not be null/empty 'name' => 'required'
filled Field must not be empty when present 'name' => 'filled'
present Field must be present but can be empty 'name' => 'present'

Conclusion

Use the required validation rule to ensure input fields are not NULL in Laravel. The Validator::make() method provides a simple way to validate data and handle validation failures appropriately.

Updated on: 2026-03-15T10:17:35+05:30

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements