Laravel 5.4 sometimes validation rules not working Laravel 5.4 sometimes validation rules not working laravel laravel

Laravel 5.4 sometimes validation rules not working


The problem occurs because of \Illuminate\Foundation\Http\Middleware\ConvertEmptyStringsToNull::class in Http\Kernel.php.

When you submit empty date field this middleware converts empty string to null. Then validation returns not valid date error. You can check docs for more details.

It can be fixed with nullable

public function rules(){    return [        'available_from' => 'sometimes|nullable|date',    ];}

From Laravel docs:

nullable

The field under validation may be null. This is particularly useful when validating primitive such as strings and integers that can contain null values.