Laravel-excel: How to validate date column in validation rules with model?

Created on 21 Mar 2019  路  7Comments  路  Source: Maatwebsite/Laravel-Excel

Versions

PHP version: 7.0.8
Laravel version: 5.5.36
Package version: 3.1

Description

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

Additional Information

Please find the below import class:

namespace App\Imports;

use AppUser;
use AppSetting;
use App\Contribution;

use IlluminateSupport\Collection;
use IlluminateSupport\FacadesValidator;
use Maatwebsite\Excel\ConcernsToModel;
use Maatwebsite\Excel\Concerns\WithHeadingRow;
use Maatwebsite\ExcelValidators\Failure;
use Maatwebsite\Excel\Concerns\Importable;
use Maatwebsite\Excel\ConcernsSkipsOnFailure;
use Maatwebsite\Excel\ConcernsSkipsFailures;
use Maatwebsite\Excel\Concerns\WithValidation;

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

public function rules(): array
{
    return [
            'email' => 'string|email|max:255|exists:users',
            'amount' => 'required|numeric',
            'contribute_date' => 'required|dateformat:d/m/Y', 
            'reference' => 'present',
        ];
}
public function onFailure(Failure ...$failures)
{
    // Handle the failures how you'd like.
}
public function model(array $row)
{
    //Handle the import code here
}

}

Most helpful comment

Hi @kannan-mkm
Yes, you can validate or change date format according to your validation before going data into model.
You have to use WithMapping concern like below.

use Maatwebsite\Excel\Concerns\WithMapping;
class ImportClass implements WithMapping
{
    public function map($row): array
    {      

        if(gettype($row['your_date_field']) == 'double'){            
           $row['your_date_field'] = \PhpOffice\PhpSpreadsheet\Shared\Date::excelToDateTimeObject($row['your_date_field']); 
        }                
        return $row;
    }
}

All 7 comments

The below rules validation is not working.

Can you specify what you mean with "not working".

I have tried to debug. It will return as a integer for example: 43398. So, it will through error.

The below value in the excel file.

Contribute Date


25/10/2018

See @2115

Thanks for your reply. But I need to validate the date format also. Is it possible?

Ok. Thanks a lot. Awaiting for new version 3.2...

Hi @kannan-mkm
Yes, you can validate or change date format according to your validation before going data into model.
You have to use WithMapping concern like below.

use Maatwebsite\Excel\Concerns\WithMapping;
class ImportClass implements WithMapping
{
    public function map($row): array
    {      

        if(gettype($row['your_date_field']) == 'double'){            
           $row['your_date_field'] = \PhpOffice\PhpSpreadsheet\Shared\Date::excelToDateTimeObject($row['your_date_field']); 
        }                
        return $row;
    }
}

Hi @kannan-mkm
Yes, you can validate or change date format according to your validation before going data into model.
You have to use WithMapping concern like below.

use Maatwebsite\Excel\Concerns\WithMapping;
class ImportClass implements WithMapping
{
    public function map($row): array
    {      

        if(gettype($row['your_date_field']) == 'double'){            
           $row['your_date_field'] = \PhpOffice\PhpSpreadsheet\Shared\Date::excelToDateTimeObject($row['your_date_field']); 
        }                
        return $row;
    }
}

Hello, guess this is too late, but at the response above you only gave for data formatting if I get it, what about the date validation. I mean after the right formatting how do you subject the data from the date field for validation?

Was this page helpful?
0 / 5 - 0 ratings