0

I and trying to upload a CSV file and validate it against data types for each column. I am using Laravel-Excel 3.1 on Laravel 5.8.

I am allowing users to upload CSVs and as such the table is generated dynamically. I build an array for the validator (`$this->validator_array') that looks like this:

array:6 [▼
  "*.0" => "date_format:d/m/Y|required"
  "*.1" => "integer"
  "*.2" => "date_format:d/m/Y"
  "*.3" => "string"
  "*.4" => "float|required"
  "*.5" => "string|required"
]  

Then in my Import class importing using collection, I call it like this:

/**
 * Import CSV into collection
 *
 * @param Collection $rows
 */
public function collection(Collection $rows)
{
    // Use Laravel to validate
    Validator::make($rows->toArray(), [
        $this->validator_array
    ])->validate();

    foreach ($rows as $rowNum => $row)
    {
        // Only start import at desired row
        if ($rowNum >= $this->start_import_row) {

            // Format insert record
            foreach($row as $colNum => $col) {
                $this->formatted_row[$this->table_headings[$colNum]] = $col;
            }

            // Save record in table
            DB::table($this->table_name)->insert($this->formatted_row);

        }

    }
}

But it fails with unrecongnized validation. My message is:

Method Illuminate\Validation\Validator::validateNumeric|required does not exist.

What did I miss?

1 Answer 1

1

If $this->validator_array is already an array, you are nesting it within another one using [$this->validator_array] so just try:

Validator::make($rows->toArray(), $this->validator_array)->validate();
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.