3

I'm using the following package https://laravel-excel.com/ to import excel files but have run into a problem while validating the rows.

The header row names might be different from the column names in the database. So my question is how can I validate the header rows with a different name? For example the database column name is first_name but the header row name can be either first_name or voornaam.

I was also wondering why the :message isn't using the localization :attribute from the language files. For example the attribute first_name is translated in lang/nl/validation.php as voornaam the error message still returns first_name.

The import function:

public function model( array $row )
{
        return new Participant([
            'salutation' => $row['salutation'] ?? $row['aanhef'] ?? null,
            'first_name' => $row['first_name'] ?? $row['voornaam'] ?? $row['voorletters'] ?? $row['voorletter'],
            'last_name' => $row['last_name'] ?? $row['achternaam']
     ]);
}

The validation rules:

public function rules(): array
{
        return [
            'salutation' => 'nullable|string',
            'first_name' => 'required|string',
            'last_name' => 'required|string',
       ]
}

I have tried to use the customValidationAttributes but that doesn't work. Also made a issue about it but so far no response.

public function customValidationAttributes()
{
    return [
        'salutation' => 'salutation',
        'aanhef' => 'salutation',
        'first_name' => 'first_name',
        'voornaam' => 'first_name',
        'last_name' => 'last_name',
        'achternaam' => 'last_name'
     ]
 }
SuperDJ
  • 7,488
  • 11
  • 40
  • 74
  • Did you ever find a solution to normalize these column names? – MrThePlague Dec 24 '20 at 03:41
  • I think that you can use public function prepareForValidation(array $row) { return [ 'salutation' => $row['salutation'] ?? $row['aanhef'] ?? null, ] } This method is called before rules() – Simion Feb 10 '21 at 09:57

0 Answers0