Laravel-excel: [PROPOSAL] Allow imports to configure the validator instance

Created on 8 Oct 2020  路  3Comments  路  Source: Maatwebsite/Laravel-Excel

Versions

  • PHP version: 7.4
  • Laravel version: 8.7
  • Package version: 3.1.23

Description

My problem:
Right now complex validation that cannot be handled through rules (e.g. $validator->sometimes(...) or $validator->after(...)) is hard to implement in imports, because there is no way to configure the validator that is being used. This causes a rift between FormRequest and Import validation (and honestly creates code depth in the import due to extra ifs and manual failures)

My proposal:
I propose to add the ability to configure the validator through a withValidator method, similar to how this is possible in a FormRequest.
Similar to FormRequest this method would be optional, so that it would only have to be implemented when its needed. The RowValidator class can check for method existence when it's creating the validator instance for an import.

Example

class UserImport implements WithValidation
{
    public function withValidator(Validator $validator)
    {
        $validator->after(function ($validator) {
            if ($this->somethingElseIsInvalid()) {
                $validator->errors()->add(...);
            }
        });
    }
}

Additional Information

  • Official Laravel documentation which outlines how this works in form requests: https://laravel.com/docs/8.x/validation#form-request-validation
  • If the proposal seems okay, I can create a PR (unless I'm missing something I don't think its an awful lot of work to implement)
proposal

Most helpful comment

Hey @axlon I'm fine with your proposal, feel free to PR!

All 3 comments

Hey @axlon I'm fine with your proposal, feel free to PR!

PR is merged. Will be included in next release. Thanks again for the great PR!

Released

Was this page helpful?
0 / 5 - 0 ratings

Related issues

contifico picture contifico  路  3Comments

disto picture disto  路  3Comments

rossjcooper picture rossjcooper  路  3Comments

matthewslouismarie picture matthewslouismarie  路  3Comments

ellej16 picture ellej16  路  3Comments