Laravel-excel: [QUESTION] How to validate the rows without saving them

Created on 17 Mar 2019  路  11Comments  路  Source: Maatwebsite/Laravel-Excel

PHP Version: 7.2
Laravel Version: 5.7
Package version: 3.1.9

I am looking for guidance on how to best approach validating the data in each row, without saving anything to the database.

To give background, I have a bulk import facility, and I want this to be dealt with in 2 stages.

  1. Run a job to validate the data, if any data fails, report this to the user and give them a chance to correct it.

If any rows fail validation, I would like to capture them as a whole so I can report them back to the user, identifying the row/reason etc

  1. If all data is valid, run a job to import the data.

Is this possible... it feels like it should be, but all examples revolve around actually importing the records as part of the flow with validation.

Thanks in advance for any guidance.

more information needed

Most helpful comment

Hey @J5Dev in that case you could use the ToCollection or ToArray method and call the Laravel validator manually. Something along these lines:

class UsersImport implements ToCollection
{
    public function collection(Collection $rows)
    {
         Validator::make($rows->toArray(), [
             '*.0' => 'required',
         ])->validate();

         // If validation fails, it won't reach the next statement.

        foreach ($rows as $row) {
            User::create([
                'name' => $row[0],
            ]);
        }
    }
}

All 11 comments

Thanks for submitting the ticket. Unfortunately the information you provided is incomplete. We need to know which version you use and how to reproduce it. Please include code examples. Before we can pick it up, please check (https://github.com/Maatwebsite/Laravel-Excel/blob/3.1/.github/ISSUE_TEMPLATE.md) and add the missing information. To make processing of this ticket a lot easier, please make sure to check (https://laravel-excel.maatwebsite.nl/3.1/getting-started/contributing.html) and double-check if you have filled in the issue template correctly. This will allow us to pick up your ticket more efficiently. Issues that follow the guidelines correctly will get priority over other issues.

The entire import is automatically wrapped in a database transaction, that means that every error will rollback the entire import. When using batch inserts, only the current batch will be rollbacked.

https://docs.laravel-excel.com/3.1/imports/validation.html#database-transactions

Thanks Glenn, but I am already aware of that, however I am trying to not have a save function at all, as described above.

I want to validate each row, and if successful, I then fire an event. I will be handling the import/save of the data in a completely separate step.

Hey @J5Dev in that case you could use the ToCollection or ToArray method and call the Laravel validator manually. Something along these lines:

class UsersImport implements ToCollection
{
    public function collection(Collection $rows)
    {
         Validator::make($rows->toArray(), [
             '*.0' => 'required',
         ])->validate();

         // If validation fails, it won't reach the next statement.

        foreach ($rows as $row) {
            User::create([
                'name' => $row[0],
            ]);
        }
    }
}

Ahaa, thank you.

Essentially pass or fail, I don't want the data inserting, so as long as I know I can replace the foreach in the example with firing the event I need to fire then I guess were all good.

Thank you for the pointer :)

Yes that should work.

Does exactly the rick, thank you.

One final question if I may, it seems that the documented ability to skip failed validation rows or grab the details of them out don't apply (Getting normal Laravel validation errors... Have I missed something, or is it not possible this way?

Edit: Should have added my example...

Here is what I have as the setup...

class ValidateConsumers implements ToCollection, WithHeadingRow, WithValidation
{
    use SkipsFailures, Importable;

    public function collection(Collection $rows)
    {
        // code here
    }

}

The trait only works in combination with the concern/interface (SkipsOnFailure). However SkipsOnFailure & WithValidation only currently works when using ToModel.

Ahh, ok, no worries, thank you.

Will look to handle it another way. Still got me to where I need to be, so thanks for the help.

You're welcome!

Closing due to resolved/inactivity. Please feel free to reopen if you need additional support.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

gamevnlc picture gamevnlc  路  3Comments

rossjcooper picture rossjcooper  路  3Comments

vandolphreyes picture vandolphreyes  路  3Comments

matthewslouismarie picture matthewslouismarie  路  3Comments

daraghoshea picture daraghoshea  路  3Comments