Code Monkey home page Code Monkey logo

laravel-csv-validator's Introduction

Laravel Csv and Excel Validator

Latest Stable Version Total Downloads Latest Unstable Version License Monthly Downloads

A Laravel 5.5 package to validate csv, excel and OpenOffice data.

The idea behind the package is simple: Just use the same validation logic and syntax as in request validating.

The package does so far only supports CSV / Excel files with a header row

Installation

Execute composer command.

composer require konafets/laravel-csv-validator:1.0-dev

Thanks to Laravels Auto-Discovery, you are done. The ServiceProvider and Alias is registered automatically.

Basic usage

$csvPath = 'employees.csv';
$rules = [
    'First Name' => 'required|string',
    'Last Name' => 'required|string',
    'Year of Birth' => 'required|numeric',
];

$csvValidator = CsvValidator::make($csvPath, $rules);

if($csvValidator->fails()) {
    $errors = $csvValidator->getErrors();
} else {
    $csvData = $csvValidator->getData();
}

Rules

$rules = [
    'First Name' => 'required|string',
    'Last Name' => 'required|string',
    'Year of Birth' => 'required|numeric',
];

In this example, the first row of the CSV need to have three columns named First Name, Last Name and Year of Birth. There might be others columns as well in your file but we don't care for them.
This keys will be used as attribute names for error message.

For more details of the rules see.

Error messages

You can get error messages after calling fails().

$errors = $csvValidator->getErrors();

foreach ($errors as $rowIndex => $row) {
    foreach ($row as $column => $messages) {
        echo 'Row ' . $rowIndex . ', Col ' . $column . ': ' . implode(',' , $messages) . '<br>';
    }
}

CSV data

You also can get CSV data after calling fails().

$csvData = $csvValidator->getData();

Exception

The Validator throws an exception when:

  • your CSV does not have any data.
  • Heading key not found.

Some example code

try {
    $csvValidator = CsvValidator::make($csv_path, $rules, $encoding);

    if($csvValidator->fails()) {
        // Do something..
    }
} catch (\Exception $e) {
    echo $e->getMessage();
}

Credits

This package is heavily inspired by https://github.com/SUKOHI/CsvValidator. They use maatwebsite/excel as the underlying Excel library, while this package uses box/sprout.

laravel-csv-validator's People

Contributors

konafets avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

laravel-csv-validator's Issues

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.