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

chimit's avatar

Text date validation

I'm trying to use text dates, but Laravel date validation always fails despite the documentation:

date The field under validation must be a valid date according to the strtotime PHP function.

https://laravel.com/docs/5.4/validation#rule-date

My validation rules:

'start' => 'sometimes|required|date',
'end' => 'sometimes|required|date|after:start',

All my attempts to use text dates which are valid dates for strtotime parser result in "The start is not a valid date." For example, last Monday or yesterday.

Can anybody tell why does this happen?

0 likes
4 replies
chimit's avatar

@bastman69 I'm using Postman to test requests. Anyway, it's a simple GET request: ?start=yesterday

Controller:

public function totals(Request $request)
{
        $this->validate($request, [
            'aggregated_by' => 'sometimes|required|in:hour,day,month,year',
            'start'         => 'sometimes|required|date',
            'end'           => 'sometimes|required|date|after:start',
            'limit'         => 'sometimes|required|integer',
            'sort'          => 'sometimes|required|in:period,amount',
            'order'         => 'sometimes|required|in:asc,desc',
        ]);

Content-Type is application/x-www-form-urlencoded.

atishrajput's avatar

first of all use either "sometime" or "required" not both together

sometime means if value is not empty then validate (value is not manadtory)

required means always validate value (manadtory)

?start=2017-04-21 (GET REQUEST)

public function totals(Request $request) {

    $this->validate($request, [

        'aggregated_by' => 'sometimes|in:hour,day,month,year',

        'start'         => 'sometimes|date_format:Y-m-d',

        'end'           => 'sometimes|date|after:start',

        'limit'         => 'sometimes|integer',

        'sort'          => 'sometimes|in:period,amount',

        'order'         => 'sometimes|in:asc,desc',

    ]);

}

1 like

Please or to participate in this conversation.