Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

galih56's avatar

Laravel date format validation always returns invalid response

I have an API built with laravel. The code below always give error message "start_on/due_on doesn't match the date format d-m-Y H:i:s". Eventhough, my webapp/insomnia sends data with the correct format

Validation Code

$fields=$request->validate([
            'project_id' => 'nullable',
            'title' => 'required|string|max:255',
            'description' => 'required|string',
            'progress' => 'numeric',
            'start_on' => 'nullable|date|date_format:d-m-Y H:i:s',
            'due_on' => 'nullable|date|date_format:d-m-Y H:i:s',
            'started_at' => 'nullable|date|date_format:d-m-Y H:i:s',
            'complated' => 'boolean',
            'completed_at' => 'nullable|date|date_format:d-m-Y H:i:s',
            'completed_by' => 'nullable|numeric',
            'tags' => 'array',
            'tags.*' => 'numeric|distinct',
            'assignees' => 'array',
            'assignees.*' => 'numeric|distinct'
        ]);

Sent data

{
			"project_id":"0",
			"title":"Testing",
			"description":"testing",
			"tags":["2"],
			"assignees":["0"],
			"start_on": "18-5-2023",
			"due_on": "25-5-2023 00:00:00"
}

Am i missing something? from the webapp/insomnia i put 00:00:00 to give set hour, minute, and second to fit the validation on due_on property. Yet, it still doesn't work

0 likes
8 replies
Snapey's avatar

date rule is evaluated before date_format

The field under validation must match one of the given formats. You should use either date or date_format when validating a field, not both.

1 like
galih56's avatar

@Snapey I removed "date" validation, it still return invalid response.

Snapey's avatar

@galih56 Then the date in your request is not in the format stated in your validation rule.

data;

"start_on": "18-5-2023",

validation

start_on' => 'nullable|date|date_format:d-m-Y H:i:s',

You are not sending H:i:s data

galih56's avatar

@Snapey Before i posted this, start_on and due_on have "00:00:00". It didn't work, so i put "date" validation and also didn't work. Is it because i use wrong format? I want to validate data something like this "31-5-2023 23:59:59" (24 hour)

Rebwar's avatar
Rebwar
Best Answer
Level 32

The d and m in the date format need to be with a leading zero. baed on your example (31-5-2023 23:59:59) looks like you are not considering leading zero for the month. If you want to use a date format without leading zeros for the day and month, you can use below format.

'start_on' => 'nullable|date_format:j-n-Y H:i:s',
1 like
galih56's avatar

@Rebwar Sorry for late response. This is the answer i was looking for. I forgot to convert data sent from my frontend to have leading zero.

Thank you so much

Snapey's avatar

so if you gave false, misleading information earlier, what does your data actually look like?

1 like
galih56's avatar

@Snapey I solved the issue based on Rebwar's answer. I had wrong format in my frontend

Thank you for your time.

Please or to participate in this conversation.