Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prewarn about possible validation issues #3

Open
simonhamp opened this issue Apr 22, 2019 · 2 comments
Open

Prewarn about possible validation issues #3

simonhamp opened this issue Apr 22, 2019 · 2 comments
Labels
enhancement New feature or request
Milestone

Comments

@simonhamp
Copy link
Owner

Like when a field is required but not yet matched up to a column

@simonhamp simonhamp added this to the v1.0.0 milestone Apr 22, 2019
@simonhamp simonhamp added the enhancement New feature or request label Apr 22, 2019
@dillingham
Copy link

dillingham commented Apr 27, 2019

In my project I added validation for each row & it has been super valuable
Appending the errors to an error column allows very easy debugging
In my project, outputs an invalid.csv which makes it easy to re-import only error rows after fixing

public function checkValidation($row)
{
    $rules = $this->importClass->getRules();

    $validator = Validator::make($row, $rules);

    $is_invalid = $validator->fails();

    if ($is_invalid) {
        $row['errors'] = implode(' ', $validator->messages()->all());

        $this->invalid[] = $row;
    }

    return $is_invalid;
}

@dillingham
Copy link

I also had a duplicate check that does the same but duplicates.csv

public function checkDuplicates($row)
{
    if (!isset($this->importClass->duplicate) || 0 == count($this->importClass->duplicate)) {
        return false;
    }

    $conditions = collect($row)->intersectByKeys(array_flip($this->importClass->duplicate))->toArray();

    $models = app($this->importClass->model)
        ->withoutGlobalScopes()
        ->withoutTrashed()
        ->where($conditions)
        ->get();

    $is_duplicate = 0 != $models->count();

    if ($is_duplicate) {
        $resource = strtolower(str_plural(class_basename($this->importClass->model)));

        $row['links'] = $models->map(function ($model) use ($resource) {
            return url("admin/resources/$resource/$model->id");
        })->implode(', ');

        $this->duplicate[] = $row;
    }

    return $is_duplicate;
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants