SpartnerNL / Laravel-Excel

🚀 Supercharged Excel exports and imports in Laravel
https://laravel-excel.com
MIT License
12.3k stars 1.93k forks source link

[QUESTION] Problem with import date validation #2719

Closed nicolastorgato closed 4 years ago

nicolastorgato commented 4 years ago

Prerequisites

Versions

Description

I'm trying to import CSV/Excel files into a table. I successfully managed to load CSV files (doing the same exact thing as the Excel), but I have a problem with Excel files, in particular with the date validation. Basically the validation always tells me that the date is not a date (but it actually is), I tried with different formats but nothing changes.

This is my code: OrdinanzaImport.php (this is the model import)

class OrdinanzaImport implements ToModel, WithValidation, WithHeadingRow
{
    public function model(array $row)
    {
        return new Ordinanza([
            'numero_verbale' => $row['numero_verbale'],
            'data_verbale'   => $row['data_verbale'],    ////here is the date field
            'cognome'        => $row['cognome'],
            'nome'           => $row['nome'],
            'codice_fiscale' => $row['codice_fiscale'],
            'citta'          => $row['citta'],
            'provincia'      => $row['provincia'],
            'data_notifica'  => $row['data_notifica'],
        ]);
    }

    public function rules(): array
    {
        return [
            'numero_verbale' => 'required|numeric|unique:ordinanze',
            'data_verbale'   => 'required|date:ordinanze',            ////here is the date validation
            'cognome'        => 'required|min:1|max:50:ordinanze',
            'nome'           => 'required|min:1|max:50:ordinanze',
            'codice_fiscale' => 'nullable|alpha_num|size:16:ordinanze', 
            'citta'          => 'required|min:1|max:50:ordinanze',
            'provincia'      => 'nullable|min:1|max:50:ordinanze',
            'data_notifica'  => 'nullable|date:ordinanze',
        ];
    }


controller.php

Excel::import(new OrdinanzaImport, $request->file('file'), null, \Maatwebsite\Excel\Excel::XLSX);


I also tried to use the following code that I found on the package issues but nothing:

'data_verbale' => \PhpOffice\PhpSpreadsheet\Shared\Date::excelToDateTimeObject($row['data_verbale'])

This is the Excel file if you want to check: https://easyupload.io/sq2ift

How could this be fixed?

Thanks!

patrickbrouwers commented 4 years ago

Validation is executed before your. model function. I don't think you can use the date: rule for it, because it's not an actual date yet. You most likely need to implement a custom rule that tries to convert the excel timestamp to a date and validate it like that.

nicolastorgato commented 4 years ago

I can't fix the problem by myself, but patience. Thanks anyway!

patrickbrouwers commented 4 years ago

What I meant with custom rules: https://laravel.com/docs/7.x/validation#using-rule-objects

If you show me what you tried, I'll try to help.

HamzaDevz commented 4 years ago

@nicolastogato see if it help you.

What I did is applying the min/max rule like that:

public function rules() : array
{
       $today = Date::timestampToExcel(today()->timestamp);
       $deliveryDateConfirmedMax = Date::timestampToExcel($this->purchaseRequest->created_at->addWeekdays(5)->timestamp);
       return [
            'delivery_date_confirmed'   => "nullable|required_unless:*.quantity_confirmed,,0|min:$today|max:$deliveryDateConfirmedMax"
       ];
}

Custom rules as @patrickbrouwers advise is better though.