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

noblemfd's avatar

How to validate date format in maatwebsite excel upload

I have created the import class using model with validation rules. I need to validate the date format for date column as YYYY-MM-DD. The below rules validation is not working. Kindly clarify how to achieve this?

class StudentImport implements ToModel, WithHeadingRow, WithValidation, SkipsOnFailure
{
    use Importable, SkipsFailures;

    public function rules(): array
    {
        return [
                'email' => 'string|email|max:255|exists:users',
        'birth_date' => 'required|dateformat:YYYY-MM-DD', 
        ];
    }
    public function onFailure(Failure ...$failures)
    {
        // Handle the failures how you'd like.
    }
    public function model(array $row)
    {
        //Handle the import code here
    }
}

The below rules validation is not working.

How do I achieve this?

0 likes
8 replies
jlrdw's avatar

@noblemfd where did you get dateformat from?

Did one of the previous laravel versions use dateformat instead of date_format.

noblemfd's avatar

@michaloravec - It doesn't work on Maatwebsite Excel Format. I got this error:

> At Row 2, The birth_date does not match the format YYYY-MM-DD

Yet it's in the format

I think the way Maatwebsite does it's date format in Excel Import is different

jlrdw's avatar

@noblemfd could you follow their documentation and see if it works. They have examples.

Also if importing I don't understand a validation rule it's an import not a user form.

You can't make the CSV file correct an error. If a form, failed validation will return the problem to the user, you can't return a session problem to a CSV file. Sorry if I don't understand.

Usually I run fields though strip_tags.

You probably just need to format the date for insert


$tdate = new \DateTime($array[$i][2]); // use your CSV field here.
$ndate = $tdate->format('Y-m-d');

Adjust as needed, usually I don't strip tags on a date field, a date field will not accept those stray string HTML characters. But I strip tags on text fields.

jukia's avatar

@jlrdw how we get 02/03/2013 this format because i have try your logic is working like this 03/02/1313 please help

jlrdw's avatar

@jukia when importing to database you need the correct format, YYYY-MM-DD.

Display it however you need.

Please or to participate in this conversation.